8

While working with pointers i wrote the following code,

int main()
{
    int a[]={10,20,30,40,50};
    int i;
    for(i=0;i<5;i++)
    {
        printf("\n%d",*a);
        a++;
    }
    return 0;
}

Now as per my understanding array name itself is an address in c and the pointer arithmetic done is here is correct as per my knowledge. But when i try to run the code it is giving me "Lvalue Required" error.

So what is the exact reason for occuring Lvalue required error because before this also i have come across situations where this error is there. Secondly why the arithmetic on the pointer is not legal here in this case?

ATR
  • 2,160
  • 4
  • 22
  • 43
  • As per my understanding @Ankur you can not assign pointer to that particular variable which is array ! – Chintan Feb 02 '12 at 09:26

3 Answers3

6

You can't do a++ on a static array - which is not an Lvalue. You need to do on a pointer instead. Try this:

int *ptr = a;
int i;
for(i=0;i<5;i++)
{
    printf("\n%d",*ptr);
    ptr++;
}

Although in this case, it's probably better to just use the index:

int i;
for(i=0;i<5;i++)
{
    printf("\n%d",a[i]);
}
Mysticial
  • 464,885
  • 45
  • 335
  • 332
  • With pointer variable i can do this i know very well but here what is the reason it is not working ? And what is the exact source of Lvalue required exception? – ATR Nov 21 '11 at 21:25
  • 2
    It's `a`. Static arrays are not Lvalues. So you can't assign anything to them. (++ and -- included) – Mysticial Nov 21 '11 at 21:26
  • :Sorry but i don't getting "Static arrays are not Lvalues". – ATR Nov 21 '11 at 21:30
  • 5
    An "Lvalue" is something that can be on the left side of an assignment (something that can be stored into). This is the case with pointers, but not the case with arrays. You can't modify `a` itself. Hence why you are getting this error. – Mysticial Nov 21 '11 at 21:33
  • 1
    so is it the case that i cannot change the array variable itself? I mean "arr=arr+1" is illegal for any array declaration?\ – ATR Nov 21 '11 at 21:36
  • 3
    Think of it this way - if you increment "a", how do you get back to the beginning of the array? You've lost the only reference you had to it. If you set a pointer to the beginning of "a" and increment the pointer, you can still find the original array by referencing "a". – Paul Tomblin Nov 22 '11 at 01:10
3

Under most circumstances, the name of an array evaluates to a value that's suitable to assign to a pointer -- but it's still a value, not an actual pointer.

This is analogous to a value like, say, 17. You can take the value 17, and assign it to an int. Once you do that, you can increment, decrement, and otherwise manipulate that int. You can't, however, do much of anything to 17 itself -- it is what it is, and can't be changed.

The name of an array is pretty much the same way. It has the right type to be able to assign it to a pointer, but on its own it's just a value that you can't manipulate. If you assign that value to a pointer, you can manipulate the pointer, but you can never do much to the original value itself -- it is what it is, and can't be changed.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Flvalue.htm

also i guess it depends on the compiler?

WindowsMaker
  • 3,132
  • 7
  • 29
  • 46
  • 2
    It doesn't depend on the compiler; it depends on the definition of the C language. The operations you're attempting don't conform to that definition. Very simple. Do what @Mysticial says and save yourself and us a lot of trouble. Then read up on pointers and stop blindly and naively accepting the statement you've obviously read somewhere that "arrays and pointers are the same". They are not. – Pete Wilson Nov 21 '11 at 21:43
  • somewhat relevant: http://stackoverflow.com/questions/2305770/efficiency-arrays-vs-pointers like Pete said, they are not the same. They may behave in the same way in some cases but under the hood not exactly the same – WindowsMaker Nov 21 '11 at 22:02