You can count on the behaviour in your first code example.
I assume it works because it's
translated to &*(a+1) and then the &
and * cancel each other, but can I
count on it, or is it compiler
specific? that is, does the standard
have any thing to say about this?
a[1]
is the same as *(a + 1)
, and &a[1]
is the same as &(*(a + 1))
, which gives you a pointer (&
) to the (dereferenced, *
) int at a + 1
. This is well-defined behaviour, which you can count on.
int *address_of_second = a+1
This is readable, but not quite as readable as &a[1]
in my opinion. &a[1]
explicitly shows that a
is a pointer, that you're referencing an offset of that pointer, and that you're getting a pointer to that offset. a + 1
is slightly more ambiguous in that the actual line doesn't tell you anything about what a
is (you can deduce that it's a pointer but for all you know from that snippet a
could be just an int
).
Even so, that's just my opinion. You're free to make up your own style decisions, so long as you understand that behind the scenes that they're the same at the lowest level.