"gokrix" <> wrote in message
news:43904025$...
> #include <stdio.h>
> #include <stdlib.h>
>
> struct s
> {
> int i;
> char c;
> float f;
> };
>
> int main()
> {
> printf("addr is [%p]. \n", &(((struct s*)0)->c));
> return EXIT_SUCCESS;
> }
>
> When I compile (gcc -g -W -Wall -ansi -pedantic) and run the above code,
> it prints "addr is [0x4].". If I change to 0 to 100, it prints 104.
>
> Why?
> Which part of the standard mandates this behaviour?
The logic bit
You're using 0 -or- 100 as an address, and then casting that to struct s
pointer. You then take the address of the member 'c' - which comes after
member 'i'. i is an int, and appears to be 32-bits with your compiler.
32-bits = 4 bytes. So, addresses 0, 1, 2, 3 are 'i', and therefore 'c'
starts at 4.
Presumably, you actually want a struct s, e.g.,
struct s j;
printf("addr is [%p]. \n", &(((struct s*)&j)->c));