-1

My question is why 2nd printf shows all same addres, but 4th shows different numbers? I compiled it under gcc 4.8.2 on intel, 64 bit on some Linux distro.

First 2 pritfs are supposed to print memory addresses ( not counting 1st number which is int value in 1st line.

Second and third to print char values from place in memory given by pointers fom 1st and 2nd printfs.

Following code

#include<stdio.h>

int main()
{
    char *ptr;
    int numer;

    numer = 0xAABBCCDD;
    ptr=&numer;

    printf("---\n%x\n %x %x %x %x\n",
    numer,
    (ptr),
    (ptr++),
    (ptr++),
    (ptr++)
    );
    ptr=&numer;


    printf("---\n%x\n %x %x %x %x\n",
    numer,
    (++ptr),
    (++ptr),
    (++ptr),
    (ptr)
    );

    ptr=&numer;

    printf("---\n%x\n %x %x %x %x\n",
    numer,
    *(ptr),
    *(ptr++),
    *(ptr++),
    *(ptr++)
    );
    ptr=&numer;


    printf("---\n%x\n %x %x %x %x\n",
    numer,
    *(++ptr),
    *(++ptr),
    *(++ptr),
    *(ptr)
    );


    return 0;
}       

Produces output:

/a.out                                                                                                                         
---
aabbccdd
 53209c77 53209c76 53209c75 53209c74
---
aabbccdd
 53209c77 53209c77 53209c77 53209c77
---
aabbccdd
 ffffffaa ffffffbb ffffffcc ffffffdd
---
aabbccdd
 ffffffaa ffffffbb ffffffcc ffffffdd

1 Answers1

0

you need to understand sequence points. that is the part of the c semantics that describes when changes are "visible" to the user.

andrew cooke
  • 45,717
  • 10
  • 93
  • 143