What is wrong with the increment operator in an array of pointers?

I just found a little confusion when using the increment operator in an array of pointers.

Code 1:

int main(void) { char *array[] = {"howdy", "mani"}; printf("%s", *(++array)); return 0; } 

When compiling, gcc throws the known error "lvalue required as an increment operand".

But, when I compile the code below, it does not show errors !!! Why?

Codex2:

 int main(int argc, char *argv[]) { printf("%s",*(++argv)); return 0; } 

In both cases, I incremented the pointer array. Therefore, it must be done in this way.

 char *array[] = {"howdy","mani"}; char **pointer = array; printf("%s",*(++pointer)); 

But why does code2 not show errors?

+5
source share
1 answer

Arrays cannot be expanded.

In the first code example, you are trying to grow an array. In the second code example, you are trying to increase the pointer.

What touches you is that when an array declarator appears in the list of function parameters, it is actually configured as a pointer declarator. (This is different from the decay of an array pointer). In the second snippet, char *argv[] actually means char **argv .

See this topic for a similar discussion.

+5
source

Source: https://habr.com/ru/post/1233987/


All Articles