GCC: Address of member pointer

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

 



Hello,

I'm a little confused about pointers in a specific case. The below code has
been tested on both gcc version 4.2.3 (Ubuntu 4.2.3-2ubuntu7) and gcc
version 3.4.5 (mingw-vista special r3).

My question is, why do output lines 3: and 5: give the same pointer value?

My assumption was that line 5: would get the address of the pointer that's a
member of mys2, but it gets the address of the struct mys2 instead.

Is this expected behaviour? And if so why?

Code:

---

#include <stdlib.h>
#include <stdio.h>

struct s1
   {
   int p;
   };

struct s2
   {
   struct s1 *mptr;
   };

int main(int argc, char* argv[])
   {
   struct s1 *mys1 = (struct s1*) malloc(sizeof(struct s1));
   struct s2 *mys2 = (struct s2*) malloc(sizeof(struct s2));
   
   mys2 -> mptr = mys1;
   
   /* Why is 3: the same as 5: ? */
   printf("1: s1:  %p\n", mys1);
   printf("2: s1&: %p\n", &mys1);
   printf("3: s2:  %p\n", mys2);
   printf("4: s2&: %p\n", &mys2);
   printf("5: s1m: %p\n", &mys2 -> mptr);
   
   free(mys1);
   free(mys2);
   }
-- 
View this message in context: http://www.nabble.com/GCC%3A-Address-of-member-pointer-tp20087495p20087495.html
Sent from the gcc - Help mailing list archive at Nabble.com.


[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