Why is arr and &arr the same?

#include <cassert>

struct foo {
    int x;
    int y;
};

int main() {    
    foo f;
    void* a = &f.x;
    void* b = &f;
    assert(a == b);
}

For the same reason the two addresses a and b above are the same. The address of an object is the same as the address of its first member (Their types however, are different).

                            arr
                      _______^_______
                     /               \
                    | [0]   [1]   [2] |
--------------------+-----+-----+-----+--------------------------
      some memory   |     |     |     |        more memory
--------------------+-----+-----+-----+--------------------------
                    ^
                    |
           the pointers point here

As you can see in this diagram, the first element of the array is at the same address as the array itself.

Leave a Comment