1

When displaying the address of an individual member of a struct/class, the displayed address is the one of the whole structure.

Indeed,

#include <iostream>

struct S {
    int x;
    int y;
};

int main() {
    S s;
    std::cout<<&s<< " --- "<<&(s.x)<<std::endl;    
}

will print e.g. 0x7ffd4e882780 --- 0x7ffd4e882780.

I found many posts that relate about manipulating individual members of a struct by using recipes such as

int S::*p = &S::x; 
S* ptr = &s;
ps->*ptr = 1000;

which may be related in some way to my question but I could not find one that explains why the individual address of x within the struct layout is the same (at least displayed) as the one of of the struct. I guess that I miss something obvious but do not find what. Would you have any idea ?

Eurydice
  • 8,001
  • 4
  • 24
  • 37

0 Answers0