Re: GCC: Address of member pointer

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




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.


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux