MC logo
Command Line Arguments
[^] CSc 220 Home
[Introduction] [Boolean and Control] [Functions] [Arrays and Pointers] [Dynamic Arrays] [Array Errors] [Command Array] [Standard Containers] [Structs and Classes] [Automatic Pointer] [Multi-File Programs] [Copying Objects] [Templates] [Inheritance] [Plain C I/O] [Type Odds And Ends] [Plain C Strings] [File I/O]
[Command Line Arguments] [Stupid Argument Tricks]
 * This program lists the command line arguments, one per line. 
#include <iostream>

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[1] is the string "with", and the expression argv[1][2] is the character 't'. Notice that argv[0] is (as always) the name of the program being run.