Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

incrementing an array of pointers in C

Tags:

arrays

c

pointers

this is probably an essentially trivial thing, but it somewhat escapes me, thus far..

char * a3[2];
a3[0] = "abc";
a3[1] = "def";
char ** p;
p = a3;

this works:

printf("%p - \"%s\"\n", p, *(++p));

this does not:

printf("%p - \"%s\"\n", a3, *(++a3));

the error i'm getting at compilation is:

lvalue required as increment operand

what am i doing wrong, why and what is the solution for 'a3'?

like image 223
XXL Avatar asked Jun 03 '11 10:06

XXL


2 Answers

a3 is a constant pointer, you can not increment it. "p" however is a generic pointer to the start of a3 which can be incremented.

like image 185
Geoffrey Avatar answered Sep 25 '22 22:09

Geoffrey


You can't assign to a3, nor can you increment it. The array name is a constant, it can't be changed.

c-faq

like image 33
cnicutar Avatar answered Sep 24 '22 22:09

cnicutar