7

In the following cases, each member has a different name or entity so why are their addresses the same?

struct B { int x; };
struct A { B b; };

int main()
{
    A obj;
    cout << &obj.b.x << endl;
    cout << &obj.b << endl;
    cout << &obj << endl;
}
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
user103214
  • 3,478
  • 6
  • 26
  • 37

3 Answers3

12

Because a pointer to a struct always points to it's first member (as the struct is laid out sequentially).

In C, does a pointer to a structure always point to its first member?

(C1x §6.7.2.1.13: "A pointer to a structure object, suitably converted, points to its initial member ... and vice versa. There may be unnamed padding within as structure object, but not at its beginning.")

NOTE: mange points out, rightfully so, that if you start adding virtual functions to your struct, C++ implements this by tacking the vtable at the start of your struct... which makes my statement (which is true for C) incorrect when you talk about everything you could possibly do with 'structs' in C++.

Community
  • 1
  • 1
Steve
  • 31,144
  • 19
  • 99
  • 122
  • 2
    Given the `cout` it seems this is more `C++` than `C`, so in general a pointer to a structure doesn't always point to its first member. (Just in case someone glances over your link and doesn't notice the difference.) – mange Oct 21 '11 at 04:40
  • Nonsense... structs work the same in C as they do in C++... and the OP's use of `cout` is really besides the point. – Steve Oct 21 '11 at 04:52
  • 1
    In `C++` a `struct` is just a `class` with default access of `public` instead of `private`. If you add a `struct` with virtual functions it will get a vtable pointer which will cause it to violate the above property. The use of `cout` shows it to be `C++`, not `C`, because `C` does not have `cout`. – mange Oct 21 '11 at 04:54
  • 1
    "C-style structs" are known as "Plain Old Data" in C++. POD structs have additional layout restrictions, such as the "pointer to initial member" quoted above. – MSalters Oct 21 '11 at 07:45
  • 1
    This will be true of any _standard layout type_ in C++11. Thus it goes beyond just PODs (though I think most people, including me, actually mean _standard layout type_ when they say POD). – edA-qa mort-ora-y Oct 21 '11 at 07:49
4

Because they're in the same place. The first element in struct A is a struct B, so they're actually in the same memory location (anything else in struct A would then be put after b).

Similarly, x is the first bit of data in struct B, so it's in the same position as the struct B.

It's very important to note that this won't always be true. Things like virtual functions will cause stuff to move. It is true in this case because they're plain classes/structs.

mange
  • 3,172
  • 18
  • 27
3

If you stand on the border of your country with a cup of coffee in your hand then your coordinates, the coordinates of the border and the coordinates of coffee cup will all have same value on a GPS device.

First child's first element happens to be at the starting address of the object. Names are for your own convenience computers work with memory addresses. You can name them whatever you want but memory layout depends on order and hierarchy of data members.

Muhammad Hasan Khan
  • 34,648
  • 16
  • 88
  • 131