It is because address of the variable a
concides with the address of the first element of array a
. You can also think of a
is &a[0]
which is clearer when we say "the address of the first element of the array").
Another example,
struct X
{
int i;
};
X x;
Here also the address of variable x
concides with the address of x.i
(which is the first element of the aggregate), so this would print 1
:
std::cout << (&x == &(x.i)) << std::endl; //1
So in your case, &a
is like &x
, and a
(or &a[0]
) is like &(x.i)
.
Note that in C++ a
and x
are both called aggregate (see my answer here: What is an aggregate?)