-2
int main()
{
  int a[]={2,3,4,5,6};
  int j;
  for(j=0;j<5;j++)
  {
    printf("%d\n",*a);
    a++; 
  }
  return;
}

gives "Lvalue required" error but

int main()
{
int a[]={2,3,4,5,6};
int *p,j;
p=a;
for(j=0;j<5;j++)
{
  printf("%d\n",*p);
  p++; 
 }
return;
}

doesn't. why????

seaotternerd
  • 6,298
  • 2
  • 47
  • 58
  • 4
    An array **is not a pointer**. As an id-expression, it is a non-modifiable lvalue. Hence, you cannot change it, similarly `a = p;` is forbidden. See http://stackoverflow.com/q/1641957/420683 – dyp Nov 23 '13 at 10:37

3 Answers3

2

Though closely related, arrays are not pointers. The name of the array is just a label to identify some allocated memory (hence, the Lvalue error when you try to modify it).

  • 2
    N.B. an array name (as an id-expression) *is* an lvalue (you can take its address), but it's not modifiable (you cannot assign something to it). – dyp Nov 23 '13 at 10:44
  • 1
    The fact that the name of the array is a label to identify some memory is not the cause of the “Lvalue required” error. In fact, a label identify some memory is actually what the `++` operator wants: It wants to operate on an object. In `int x`, the `x` is a label for an `int`, and `x++` works just fine. The actual problem is that the array is automatically converted to a pointer, and the result is a mere value, not an object. If the array were not automatically converted to a pointer, then `a++` would yield an error because `a` is not modifiable, not because it is not an lvalue. – Eric Postpischil Nov 23 '13 at 12:23
  • @EricPostpischil You're right, it's slightly different for C++, where I "come from" -- in C++'s postfix increment, the *modifiable lvalue* requirement comes first, *then* the requirement on the type, and no array-to-pointer conversion precedes this requirement. – dyp Nov 23 '13 at 12:29
  • @EricPostpischil Ok, maybe I was not very precise. I was just trying to find a simple way of expressing the idea of the array name being equivalent to a memory location (a number indicating an address), as opposed to a variable. As you stated perfectly well, the problem is not the non-modifiable part, but the not being an lvalue. – José M. Benítez Nov 23 '13 at 13:02
1

An array is not a pointer. In most expressions, an array is converted to a pointer automatically. The result of this conversion is no longer the array; it is just a pointer value.

The ++ operator cannot operate on a mere value. It must have an object to act on.

For example, consider int x = 3; (x+5)++;. The result of x+5 is 8. It is not x. The result is just a value, not an object, so there is no object containing 8 that ++ can operate on. This is an error.

Similarly, if a is an array of int, then a++ is equivalent to ((int *) a)++. The ++ is not trying to act on the a; it is trying to act on the result of converting a to a pointer.

An array expression is always converted to a pointer to the first element except when the array expression is the operand of sizeof, &, or _Alignof or is a string literal used to initialize an array.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • but when i had incremented array name using ++ operator in the a difffernt program then it was working fine in it – user2645461 Nov 26 '13 at 09:45
  • in it I had passed the array to some other function and then incremented it – user2645461 Nov 26 '13 at 09:46
  • like void foo(int b[][3]){ ++b; b[1][1]=9} – user2645461 Nov 26 '13 at 09:47
  • It had modified the value of array in main() which was sent as a parameter to the function foo. – user2645461 Nov 26 '13 at 09:48
  • @user2645461: The code in `main` does not attempt to modify the array. The assignment `p=a;` assigns the value of a pointer to the array to the pointer `p`. Then `p++` modifies the pointer. Unlike arrays, pointer objects are not converted to anything; they are already pointers. So, in `p++`, `p` provides an object, not just a value, that `++` can work on. – Eric Postpischil Nov 26 '13 at 12:27
0

This will display all the array data.

#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;

int main()
{
    int a[]={2,3,4,5,6};
    int *p,j;
    p=a;
    for(j=0;j<5;j++)
    {
         printf("%d\n",p[j]);
     }
   return 0;
}
Anson Tan
  • 1,256
  • 2
  • 13
  • 34