I am reading a linux programming with c language book and it has the following code.
int main(int argc, char *argv[])
{
int opt;
while ((opt = getopt(argc, argv, "iv")) != -1) {
switch (opt) {
case 'i':
opt_ignorecase = 1;
break;
case 'v':
opt_invert = 1;
break;
case '?':
fprintf(stderr, "Usage: %s [-iv] [<file>...]\n", argv[0]);
exit(1);
}
}
argc -= optind;
argv += optind;
}
I don't understand that why argv += optind works. When i tried at another program, which is the following:
int main(int argc, char* argv[])
{
char* test[] = {"Hello", "World", "I"};
argv += 1;
test += 1;
printf("%s", test[0]);
return 0;
}
the gcc compiler shows error
a.c:8:9: error: assignment to expression with array type
8 | test += 1;
the += on argv works but doesn't work with test. I don't understand which part I did wrong..
char* argv[]as a function parameter is a different way to writechar** argv. So, it is a pointer to a pointer,testis an array of pointer