Arun Rajasekar
Arun Rajasekar

Reputation: 67

Increment int pointer value - Doesn't work

I'm trying to print an int array, but it gives me an error.

error: lvalue required as increment operand

int *a[] = {1,2,3,4,5};
for(int i=0;i<5;i++)
{
    printf("%d",*a);
    a++;
}

Upvotes: 0

Views: 99

Answers (1)

eesiraed
eesiraed

Reputation: 4654

int *a[] means an array of pointers to ints. You want an array of ints here, so use int a[].

You can't increment a because a is an array, not a pointer. Arrays sometimes decay to pointers to the first element, but you can't modify that pointer. Instead, make a pointer pointing to the elements in the array like int *p = a or use the subscript operator like a[i] to access the elements.

Upvotes: 4

Related Questions