Let's think about what this means:
&(A[0])->next
It is the address of the next
pointer (not where it points, but the address of the pointer itself). And the next
pointer is the first element of struct a
, so the address of next
is the same as the address of its enclosing a
.
Therefore, the expression is the address of the struct a
referred to by A[0]
. In your original code, you never assign anything there, so it's simply a garbage value being printed. As @alk points out in another answer, you could initialize the two pointers in your variable A
and then you would see the first of those values being printed (say, 0x0).
By the way, if you want to quickly initialize A
, do it this way, not with the more verbose memset()
:
struct a *A[2] = {0};
It does the same thing (sets the two pointers to 0).
While the value being printed is garbage, the code may not be illegal. This may seem surprising, but see here: Dereferencing an invalid pointer, then taking the address of the result - you've got something similar, though admittedly you've taken it a step further by dereferencing a member of a struct as opposed to simply using *
. So the open question in my mind is: given that &*foo
is always legal when foo
is a pointer (as shown in the above link), does the same hold true for &foo->bar
?