3

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.

Ryan Leach
  • 4,262
  • 5
  • 34
  • 71
  • Thanks Peter, i should have noticed the code block function. – Ryan Leach Sep 29 '10 at 10:47
  • 1
    possible duplicate of [Pointer arithmetic and arrays: what's really legal?](http://stackoverflow.com/questions/2383837/pointer-arithmetic-and-arrays-whats-really-legal), [Are pointers and arrays any different in C?](http://stackoverflow.com/questions/1054247/are-pointers-and-arrays-any-different-in-c), and more. – Péter Török Sep 29 '10 at 10:48
  • sorry, I had no idea for what search terms to use. – Ryan Leach Sep 29 '10 at 11:24

4 Answers4

8

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
  • 22,475
  • 4
  • 52
  • 71
  • 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 Leach Sep 29 '10 at 11:03
  • 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 Sep 29 '10 at 11:08
  • 3
    @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 of all 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 Sep 29 '10 at 11:10
  • @Steve: Didn’t know that either. So I should have written "modifiable lvalue" and everything was right. – Sven Sep 29 '10 at 11:15
  • @Sven: yes, I agree with everything else you say. – Steve Jessop Sep 29 '10 at 11:28
  • @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 Sep 29 '10 at 13:29
7

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
  • 91,295
  • 49
  • 239
  • 345
2

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
  • 13,345
  • 8
  • 56
  • 105
0

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
  • 499
  • 1
  • 4
  • 7