argv
is actually a pointer, not an array. In both C and C++, a function parameter cannot be an array. If it looks like an array, it's quietly "adjusted" at compile time to be a pointer to the array's element type.
This:
int main(int argc, char *argv[])
really means this:
int main(int argc, char **argv)
When your program is invoked, argv
will be initialized to point to a char*
object. That object is the initial element of an array of char*
pointers, each of which points to (the initial element of) a string -- except that the final element, argv[argc]
contains a null pointer.
argv[0]
is a pointer to a string that represents the name of the program. If the program is invoked with two arguments, argv[1]
and argv[2]
will point to those arguments.
Finally, if you print a char*
value using std::cout << ...
, it will print, not the pointer value itself, but the value of the string that it points to.
When I run the program with arguments foo
, and bar
, argv[0]
is foo
and argv[1]
is bar
.
Are you sure about that? (Update: That was a typo in the question, now corrected.) If your program is named "my_program", the output of my_program foo bar
should be something similar to:
0 my_program
1 foo
2 bar