hi,
the following program is not running and gives CANNOT CONVERT INT TO INT* IN MAIN FUNCTION error

#include<stdio.h>
		  void main()
		  {
				int *p;
				p=0x2000;
				++p;
				printf("%d",p);
		  }

how to solve this

Edited 5 Years Ago by Narue: Added code tags

float f; # can hold only a float type
int i;  # can hold only int type
int *p; # can hold only the memory address of an int

Is 0x2000 the memory address of an int?

By the way, the rules of the C programing language says that main() must return an int, when run in a hosts operating system.
Which means void main() is illegal.

int main(void) {

# code here

return 0;
}

That's a good template for now.

Edited 5 Years Ago by Aia: n/a

I may be wrong, but I think the OP wants to demonstrate that the value of the pointer is
increased by 4 (or whatever sizeof(int) is in his machine) when he uses operator ++ on it.

This should do it:

#include<stdio.h>

int main()
{
    int * p = (int *) 0x2000;

    printf("%x", ++p);

    return 0;
}

*sigh*

Here's a correct and portable program that does what r0shi and possibly the OP wanted:

#include <stdio.h>
#include <stddef.h>

int main(void)
{
    int a[2]; // Guarantee that the addresses exist
    int *p = a;
    int *q = p++;
    
    // Casting to char* to get a byte count
    ptrdiff_t diff = (char*)p - (char*)q;

    // Notice %p and the cast to void*, both are required
    printf("%p - %p = %td\n", (void*)q, (void*)p, diff);

    return 0;
}

>int * p = (int *) 0x2000;
Creating a pointer to a random value is not portable. I really don't see the need to use a random address when you could create a variable and point to it. This is what I would expect to see:

int i;
int *p = &i;

++p; // Bzzt! Wrong!

But it's actually undefined due to generating an address with out of bounds arithmetic. ++p doesn't produce a valid address because i is a scalar variable rather than an array. To be strictly correct you'd need at least two guaranteed addresses in the same array:

int a[2];
int *p = a;

++p; // OK

>printf("%x", ++p);
This is not portable. While pointers are allowed to be converted to integer types (unsigned int in the case of %x), there's no guarantee that you won't generate a trap representation or mangle the value. Thus, %x is inappropriate for printing pointers. You need %p.

Note also that %p expects a pointer to void. While you can generally get away without the cast, it's not portable.

Edited 5 Years Ago by Narue: n/a

> ptrdiff_t diff = (void*)p - (void*)q;
I thought any kind of arithmetic on void* pointers was undefined.

$ gcc -W -Wall -ansi -pedantic foo.c
foo.c: In function ‘main’:
foo.c:10: warning: pointer of type ‘void *’ used in subtraction

If you want the number of bytes, then ptrdiff_t diff = (p - q)*sizeof(*p); might be a better choice.

Comments
+1
Always undefined!

I think you run circles: if you want to know how much *int increase by pointer increment in a C implementation it is equivalent to finding sizeof int which is easy to find and there must be one standard constant for that defined somewhere.

if you want to know how much *int increase by pointer increment in a C implementation it is equivalent to finding sizeof int

This is assuming you know that pointer arithmetic is adjusted for the size of the pointed to type. If you don't know, you won't make that connection. If you just learned the rule, you might want to test it out. That's the point of the program.

Here's a correct and portable program etc etc...

Ah, right. Thanks for the corrections. In general, I'm happy to just get my programs to compile and run on my pc. I don't like to dig into
portability or standard compliance issues because it really makes my head hurt >:( I prefer to leave that kind of stuff to smarter people...

Edited 5 Years Ago by m4ster_r0shi: n/a

This is assuming you know that pointer arithmetic is adjusted for the size of the pointed to type. If you don't know, you won't make that connection. If you just learned the rule, you might want to test it out. That's the point of the program.

I would be most confused with all those pointer tricks if I would be just beginning to learn pointers (as I still get confuced around 50% of time).

I would be more happy to confirm that with code like:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n[] = {1, 2};
    int *pn;

    pn = &n[0];
    pn++;
    if(pn == &n[1]) printf("pointer advanced to next integer");
    return 0;
}

I would be most confused with all those pointer tricks

You must be easily confused then. Your code replaces subtraction with equality (an understanding of pointer basics is still necessary), and avoids the problem of printing a pointer by eliminating that information in favor of a message. I fail to see how that's sufficiently simpler to avoid confusion.

pointers are supposed to handle already allocated memory. That can be either by user or predefined like VGA memory. But you assigned the address 0x2000. what's the meaning of that. That address belongs to which data type. By pointer arithmetic theory ++ means current address plus no. of bytes allocated for the data_type in which pointer declared.

This article has been dead for over six months. Start a new discussion instead.