What does char * argv[] means?
argv
is an array of char*
. Doing ++argv
means accessing the next cell of the array. The *
indicates we want the value of the cell, not the address.
When a program starts, it gets it's argument in the main function. That's why you ususally write.
int main(int argc, char *argv[])
This simply means that argv is a pointer to as many argument strings as indiciated by argc (== argument count). Since argv decays to char **argv you can also increase it, or you it otherwise like a pointer.
So if you want to print all arguments from the commandline:
int main(int argc, char *argv[])
{
for(int i = 0; i < argc; i++)
printf("%s\n", argv[i]);
for(int i = 0; i < argc; i++)
printf("%s\n", argv++);
return 0;
}
The parameter char * argv[]
decays to a pointer, char ** argv
. You can equally well write the function signature for main()
as:
int main(int argc, char ** argv)
You can do what you like with the pointer argv
within main()
, so argv++
for example just bumps argv
to point at argv[1]
rather than argv[0]
.
argv ---> argv[0] ---> "program"
argv[1] ---> "arg1"
argv[2] ---> "arg2"
... ...
argv[argc] == NULL