Command Line Arguments
* This program lists the command line arguments, one per line.
using namespace std;
// This declaration of main accepts what is essentially an array of strings.
int main(int argc, char *argv)
// Print the args.
for(int m = 0; m < argc; ++m)
cout << argv[m] << endl;
The parameters to main pass the program an array of strings.
The declaration of argv is
“array of pointers to character,” with the size
given by argc. We delay a full discussion of plain C strings
until later, but they are implemented as arrays of characters.
As you know, a pointer to the first member of an array is all you need
to use the data in the array. So, the declaration of argv
can be viewed as an array of pointers, each to the start of an array
of characters which forms a plain C string. Effectively an array of strings.
If we run an exacutable creatively named command as:
command with some arguments
the system sets argc and argv as:
Each member of argv is a plain-C string, which is an array of
characters. The expression. argv is the string "with",
and the expression argv is the character 't'.
Notice that argv is (as always) the name of the program