GCC: Address of member pointer

Duane Ellis duane-gcc-help@duaneellis.com
Tue Oct 21 10:56:00 GMT 2008


barry> I'm a little confused about pointers in a specific case. ...[snip]
barry> My question is, why do output lines 3: and 5: give the same 
pointer value?

[snip]
barry> My assumption was that line 5: would get the address of the 
pointer that's a
barry> member of mys2, but it gets the address of the struct mys2 instead.
barry> Is this expected behaviour? And if so why?

It is expected, and this is why.

 >   printf("3: s2: %p\n", mys2);

This prints the starting address of  struct 2 - that is obvious.

 > printf("5: s1m: %p\n", &mys2 -> mptr);

To answer your question, why is this 3 & 5 the same - ask this question:
What is the byte offset of the 1st member of a structure?
The answer is ZERO of course, and what is the value of "PTR + ZERO"?

The next question is: What is the byte offset of the 2nd element of a 
structure?
The answer is
    (1) the size of the first element.
    (2) PLUS any required alignment of the current element.

Consider:

   struct {
       char         c;
       uint32_t   x;
  };

In the above - try various types for the member X.

The last question is: What is the size of the total structure?
   The answer is: The above - rounded to the alignment of the most 
restrictive member of the structure.

-Duane.



More information about the Gcc-help mailing list