On Sun, 24 Dec 2006, Arun G Nair wrote:

> Hi,
> 
> Am a bit confused by the output of the this C program:
> 
> -------------------------------------------------------------ptr.c---
> #include <stdio.h>
> 
> int
> main()
> {
>     int *ptr, x;
> 
>     x = 2;
>     ptr = &x;
> 
>     printf("x=%d, *ptr=%d, ptr=%p, &x=%p\n", x, *ptr, ptr, &x);
> 
>     *ptr++;
>     printf("x=%d, *ptr=%d, ptr=%p, &x=%p\n", x, *ptr, ptr, &x);
> 
>     ++(*ptr);
>     printf("x=%d, *ptr=%d, ptr=%p, &x=%p\n", x, *ptr, ptr, &x);
> 
>     return 0;
> }
> 
> --------------------------------------------------------------------------
> 
> The output I get is this:
> 
> $ ./a.out
> x=2, *ptr=2, ptr=0xbfbfece0, &x=0xbfbfece0
> x=2, *ptr=-1077941020, ptr=0xbfbfece4, &x=0xbfbfece0
> x=2, *ptr=750764012, ptr=0xbfbfece5, &x=0xbfbfece0
> ---------------------------------------------------------------------------
> 
> If ++(*ptr) is supposed to increment the value *ptr, then why is there a
> change in memory address (0xbfbfece5) ?
> 
> 
> Any ideas ? I know that am referring someone else's memory, but still..

Likely ptr is pointing at itself after the *ptr++;

        -Otto

Reply via email to