error in program

following code seems to be fine for me.But it is givving me an error of
Lvalue required
..can anyone please tell me what is wrong with the code

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<stdio.h>

int main()
{
    int a[] = {10, 20, 30, 40, 50};
    int j;
    for(j=0; j<5; j++)
    {
        printf("%d\n", a);
        a++;
    }
    return 0;
}
closed account (zb0S216C)
You can't increment/decrement "a" because it's not a pointer. When "a" is used by itself without dereferencing, it evaluates to an r-value address which cannot appear in expressions that alter its value. Use the sub-script operator if you want to access an array:

1
2
3
4
5
6
7
8
int main()
{
    int a[] = {10, 20, 30, 40, 50};
    int j;
    for(j=0; j<5; j++)
        printf("%d\n", a[j]);
    return 0;
}

Wazzak
Last edited on
Topic archived. No new replies allowed.