Search code examples
cpointerslvalue

C programming; lvalue required


Possible Duplicate:
Lvalue required error

I am having an error in my C program

main () {
    int arr[] = {1, 2, 3, 4};
    printf("%d", *arr);
    arr++;
    printf("%d", *arr);
}

When I compile this code I get lvalue required error. for the line with arr++. Any Help!


Solution

  • The operand of the pre- and postfix versions of ++ and -- must be a modifiable lvalue. Unfortunately, array expressions such as arr are non-modifiable lvalues, hence your error message.

    If you want to walk the array using a pointer expression, you'll have to declare a second pointer and set it to point to the first element of the array.

    int *p = arr; // or &arr[0] - in this context they evaluate to the same thing