tags:

views:

175

answers:

4

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.

+8  A: 

a += 2; is invalid because += operator isn't defined for arrays. Furthermore arrays are non modifiable lvalues so you cannot assign to them.

Prasoon Saurav
+2  A: 

When you pass a to a function where a pointer is expected, the address of a is used. This leads to the wrong statement, an array and a pointer are interchangeable.

But

  • a is an array
  • pa is a pointer

Since pa is a scalar, you can modify it with

pa = pa + 2;

or

pa += 2;

The array a does not define any operation like

a = a + 2;  /* invalid */
harper
+8  A: 

a += 2 gets translated to a = 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.

Sven
Thankyou, you have answered my question clearly and precisely.However I now wish I remembered/(was taught) what lvalues were.I believe my confusion was that arrays were pointers to the first value.
Ryan The Leach
A lvalue basically is something that can be on the left side of an assignment statement (l for left). So basically variables (except arrays), pointer dereferences (*ptr), array element access (array[index]) and struct field access (str.field or ptr->field) are lvalues.
Sven
@Sven: Actually it is an lvalue, but one that you can't assign to. 6.3.2.1/1, "A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a const-qualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element ofall contained aggregates or unions) with a const-qualified type". Arguably, the term "lvalue" is therefore misleading terminology, since there are lvalues which cannot be the lhs of an assignment. C++0x "clarifies" the issue by introducing several new kinds of expression ;-)
Steve Jessop
@Steve: Didn’t know that either. So I should have written "modifiable lvalue" and everything was right.
Sven
@Sven: yes, I agree with everything else you say.
Steve Jessop
@Ryan The Leach: Arrays *aren't* pointers, but it often looks as if they are, because in almost all contexts they are evaluated as a pointer to their first element.
caf
A: 

when you write a += 2 then it translated to a = a + 2.

So it means you modify base address of array. That is not allow in c because if you modify base address of array then how you access array element.

It will give Lvalue required error at compile time.

Anand Kumar