Writes the following error - "lvalue required as increment operand".

#include <stdio.h> char *p[]; int main(){ p[0] = "12345"; p[1] = "qwerty"; p[2] = "l1l3433"; int i; for(i = 0; i < 2; i++){ printf("%s\n", *++p); /* здесь ошибка */ } return 0; } 

Explain, please, why?

    2 answers 2

    You have defined an array with the name p , and you cannot change the identifier, which is the name of the array. The name of the array always remains the name of the array - it is not a pointer ...

    Could you write

     char **p; 

    and it would compile. But it would not work. Neither would your code work, even if the name of the array was considered lvalue - because you declare an array with an unknown number of elements.

    You can fix the situation:

     char *p[] = {"12345","qwerty","l1l3433"}; int main(){ int i; char**pp = p; for(i = 0; i < 2; i++){ printf("%s\n", *++pp); } return 0; } 

    But here is the question: you first increase the pointer, and only then dereference ... You are right asking to withdraw only 2 elements, so do not go beyond the boundaries, but you want to bring out the second and third lines? If yes, then that's right, but if the first and second are, then ++pp should be replaced by pp++ .

    • That's right, second and third. Thank! - Egor Filatov

    Try *++p on **(++p) .
    I suspect that the compiler is trying to decode the value "++" with the "*" operator, which is impossible, since "++" is not a pointer.

    ADDED: "* p []" is a pointer to a pointer.

    • if I write ** (++ p), the character will be displayed, but this is not exactly what I need. - Egor Filatov
    • I understand you, I'm sorry. - user263096