Increment int pointer value - Doesn't work

Arun Rajasekar

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++;
}
BessieTheCow

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

setting value doesn't work when iterating array by pointer increment

LINQ increment value doesn't work

Why does derefenced pointer increment/decrement doesn't work?

char pointer doesn't increment?

Why doesn't the post increment operator work on a method that returns an int?

Increment jQuery variable value doesn't seem to work properly

Increment and decrement doesn't work

why swapping value using pointer doesn't work?

C printing double type pointer value doesn't work

Swift3: Converting raw pointer to Int (pointer itself being the int value, doesn't point anywhere!)

Copies of a pointer into a pointer doesn't work in C

Why doesn't PHP Increment work as expected

Increment number in ANGULAR doesn't work

VHDL Increment Signal doesn't work properly

Customized Increment Arrows on Input doesn't work! How to collect the input value through buttons?

Int doesn't work in struct

Increment a pointer with a fixed value

Lua C API doesn't work with pointer

golang pointer in range doesn't work

Checking NULL Pointer In C Doesn't Work

Pointer to struct doesn't work properly?

Overloading operator< doesn't work for pointer of a claas

null check of function pointer doesn't work

Why does adding to a pointer with += work, but pointer + 1 doesn't?

Pointer to pointer doesn't work in tree copying algorithm

Pointer doesn't return the right value

Why doesn't increment operator work on chars always?

unary increment operator within a string doesn't work

SwiftUI - Increment variable in a ForEach loop doesn't work for some reason