tags:

views:

143

answers:

3

Hello , I am trying some programs in c face a problem with this program can any one tell me what is the problem with this and i also want to know that when in the above case if the pointer value is incremented then will it over write the previous value address as

#include<stdio.h>
int main()
{
    int a=9,*x;
    float b=3.6,*y;
    char c='a',*z;
    printf("the value is %d\n",a);
    printf("the value is %f\n",b);
    printf("the value is %c\n",c);
    x=&a;
    y=&b;
    z=&c;
    printf("%u\n",a);
    printf("%u\n",b);
    printf("%u\n",c);
    x++;
    y++;
    z++;
    printf("%u\n",a);
    printf("%u\n",b);
    printf("%u\n",c);
    return 0;
}

suppose that the value we got in the above program (without the increment in the pointer value )is 65524 65520 65519 and after the increment the value of the pointer is 65526(as 2 increment for the int ) 65524(as 4 increment for the float ) 65520(as 1 increment for the char variable )

then if in that case will the new pointer address overwrite the content of the previous address and what value be contained at the new address

+4  A: 

You can't just increment x,y,z like that.
You have no guarantee that they are laid out in memory in that order - the memory address after 'x' could be anything.

Edit just to clarify: Yes you can increment a pointer, but you have to be sure that you 'own' the memory that you are then pointing to - typically memory in a string or an array.

You can't assume that separate variables are adjacent to each other in memory - in fact different types will be probably be spaced out so that each new one starts at a 4byte boundary.

Martin Beckett
you mean that the pointer value is not to be incremented .....?
sandy101
+1  A: 

If you're trying to output the values of the pointers x, y and z, then you need to change the last six printf statements like so:

printf("%p\n",x);

printf("%p\n",y);

printf("%p\n",z);

x++;

y++;

z++;

printf("%p\n",x);

printf("%p\n",y);

printf("%p\n",z);

Printing them out in this way will print their addresses, and you should see their address change after you increment them.

However, if you attempt to access the values stored in them after you increment them, you'll either get garbage or your program will crash. When you increment the value of a pointer like that, it will point to the next location in memory. When the pointer points to an array it will point to the array's next cell. However, your pointers do not point to an array, so the address they point to will probably not be valid.

Daniel Bingham
pointers should be printed with %p, not %u
Hasturkun
@Hasturkun Thanks, just knew it wasn't right for float or int assumed it was pointer with out double checking myself. Been so long since I've had to print a pointer, fixed now ;)
Daniel Bingham
+2  A: 

First of all, I assume your calls to printf are supposed to display the values of x, y and z, or the addresses of a, b, and c, correct? In that case you need to change them to:

printf("%p\n", x);
printf("%p\n", y);
printf("%p\n", z);

These will take into account the size of a pointer on your processor, if that is different from the size of an int. They will also print the address in hexadecimal, which is much more common since addresses can get quite large.

and after the increment the value of the pointer is 65526(as 2 increment for the int )

Assuming you're on a platform where int is 2 bytes (on most modern home PCs int is probably 4 now).

then if in that case will the new pointer address overwrite the content of the previous address and what value be contained at the new address ......plz help

You haven't actually dereferenced any of the pointers here. So the memory occupied by a, b, and c is still untouched; all you've done is changed what x, y, and z are pointing to. If you were to dereference one of the pointers after incrementing it, you would be modifying memory past the variable it initially pointed to. So if a is at address 65526 decimal and int is 2 bytes:

int *x = &a; // x points to the integer at "65526" (a)
x++;         // x points to the integer at "65528", a is still at 65526
*x = 5;      // you've just modified the memory at addresses 65528 and 65529.

If 65528 or 65529 contained some other important data (like b or c) you've just corrupted your program's state.

Nick Meyer