From my lecture slides, it states:
As illustrated in the code below an array name can be assigned
to an appropriate pointer without the need for a preceding & operator.
int x;
int a[3] = {0,1,2};
int *pa = a;
x = *pa;
x = *(pa + 1);
x = *(pa + 2);
a += 2; /* invalid */
Why is a += 2; invalid?
Can anyone help clarify?
Also feel free to edit the title if you think of a better one.
a += 2gets translated toa = a + 2. Adding a number to an array is the same as adding a number to a pointer which is valid and yields a new pointer.The assignment is the problem – arrays are not lvalues, so you cannot assign anything to them. It is just not allowed. And even if you could there is a type mismatch here – you’re trying to assign a pointer to an array which does not make sense.