6

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'?

2
  • The a3 variable is declared as an array of characters, I think you want to declare it as an array of pointers, so it should read char *a3[2] instead of char a3[2] Commented Jun 3, 2011 at 10:46
  • that was a mistake of copying over the code, you are right, yes, it was a pointer array, thank you for pointing it out Commented Jun 3, 2011 at 10:51

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

c-faq

2 Comments

@XXL The way you declared it, a3 is not a pointer, period. You're probably missing a * in your question too (char *a3[2] ?)
it was missing an asterisk due to a failed paste, what i wanted to hear was that "the array name is constant, therefore it can not be modified" - which explains it well enough. you didn't have that part in your original reply, as it appeared only after you've edited your post - that is why i have asked a follow-up question of "why". anyhow, it has been addressed by now, thanks
0

Try

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

In C, a char array[] is different from char*, even if you can use a char* to reference the first location of an array of char.

aren't both "p" and "a3" pointers to pointers?

Yes but a3 is constant. You can't modify it.

Comments

0

a3 is a name of an array. This about it as a constant pointer.

You cannot change it. You could use a3 + 1 instead of ++a3.

Another problem is with the use of "%s" for the *(++a3) argument. Since a3 is an array of char, *a3 is a character and the appropriate format specifier should be %c.

1 Comment

a3 is an array of char * so that syntax should be ok...I misread it at first too...or maybe it was edited after you posted this.
0

You cannot increment or point any char array to something else after creating. You need to modify or access using index. like a[1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.