The OS sets up the data structure with the command line strings. Then your program accesses those strings. Here is how many C programs start out:
void main( int argc, char *argv[] ) { . . . }
The OS calls main with parameter argc
set to
the number of command line parameters
and argv
containing a pointer to the array of pointers.
char *argv[]
means that argv
is
an array of pointers to char
.
(In detail: argv
is the address of the array.)
The first cell of the array argv[0]
points to the first null terminated
string.
The second cell of the array argv[1]
points to the second null terminated
string. This is the first parameter after the program name.
Here is a program that echoes to the screen the first parameter from the command line:
#include <stdio.h> void main( int argc, char *argv[] ) { printf("Argument Count: %d\n", argc ); printf("First Param: %s\n", argv[1] ); }
Here is a run of that program:
Unix, Linux, and Mac have similar behavior.