Reputation: 385144
Given a user-defined type A
, and a pointer A* a
, what is the difference between *a
and a[0]
?
(Though *(a+0)
/a[0]
are defined to be equivalent, the same is not the case for *a
/a[0]
, where a subtle difference may cause a compilation error in certain circumstances.)
Upvotes: 14
Views: 1343
Reputation: 263118
If A
is an incomplete type, *a
works, but a[0]
does not, in this example:
struct A;
void foo(A& r)
{
}
void bar(A* a)
{
foo(*a);
foo(a[0]); // error: invalid use of incomplete type ‘struct A’
}
That's because a[0]
is equivalent to *(a+0)
, but you cannot add something to a pointer to an object of incomplete type (not even zero), because pointer arithmetic requires the size to be known.
Upvotes: 36