home *** CD-ROM | disk | FTP | other *** search
-
- Page 1 of 3
-
- A R G C & A R G V
- The Command Line Variables
-
- By
-
- S. R. Sampson
- Compuserve 75136,626
-
- Recently there was a discussion concerning pointers in 'C' and
- the question was asked:
-
- Why do some programs use **argv and others use *argv[]?
-
- This article will explain the meanings of these two declarations
- and show why they can be considered synonamous.
-
- Wherever you see **argv, you can substitute *argv[]. They are
- both the same:
-
- char **argv; In English, argv is a pointer to a
- pointer to a character.
-
- char *argv[]; In English, argv is an array of
- pointers to characters.
-
- Initially we have what appears to be two different declarations
- both visually and when written in English. In the first example
- it is implying that there is only one pointer to a character. In
- the second example it is implying that there are one or many
- pointers to characters; or an array of pointers.
-
- Let's go through an example and see how the command line is
- operated on and stored in memory.
-
- For instance let's say an operator typed:
-
- cc -O test.c
-
- Which would look like this in memory:
-
- _____________________________________________________
- /___________________________________________________/|
- | c | c | | - | O | | t | e | s | t | . | c | \n |/
- ----------------------------------------------------
-
- When you execute a 'C' compiled program the first thing that
- executes is the run-time library which is attached to every
- program. This will examine the command line and search for a
- space delimiter between options or a newline character to signify
- the end of the command. The run-time will gather each option
- into character arrays (strings). For instance our example will
- produce the following strings:
-
- "cc", "-O", "test.c"
-
-
- Page 2 of 3
-
-
- A R G C & A R G V
- - The Command Line Variables....
-
-
- These strings are stored in memory and will appear like so:
-
- _________________________________________________________
- /_____________/____________/____________________________/|
- | c | c | \0 | - | O | \0 | t | e | s | t | . | c | \0 |/
- --------------------------------------------------------
- ^ ^ ^
- | | |
- 0900 0903 0906
-
-
- The run-time also makes an array of pointers which will point to
- the start of each string. These pointers will be stored in
- memory also. Let's call the array argv. In our sample command
- line we have three string arrays, so we will also have three
- pointers. The examples are using imaginary memory locations for
- storage.
-
- argv[0] = 0x0900 which points to "cc"
- argv[1] = 0x0903 which points to "-O"
- argv[2] = 0x0906 which points to "test.c"
-
- and will look like this in memory:
-
-
- argv[0] argv[1] argv[2]
- ________________________________
- /__________/_________/_________/|
- | 09 | 00 | 09 | 03 | 09 | 06 |/
- -------------------------------
- ^ ^ ^
- | | |
- 0950 0952 0954
-
-
- The memory pointer 0x0900 is a pointer to a character. Well, we
- have to store this pointer somewhere, Let's put it in memory
- location 0x0950, as illustrated above, and put the next pointer
- in location 0x0952 and finally the third in 0x0954.
-
-
-
- Page 3 of 3
-
- A R G C & A R G V
- - Command Line Variables....
-
-
- Question: What does memory location 0x0950 have in it?
-
- Answer: It has 0x0900 - a pointer to a character.
-
-
- So 0x0950 is a pointer to - a pointer to a character:
-
- char **argv;
-
- You can see now how the declaration has meaning, and you can also
- see that it does not fully describe the true state of things.
- That is, there may be more pointers than just one. Many people
- like the array declaration which tends to describe the true
- structure of most command lines:
-
- char *argv[];
-
- The operation of the run-time package finally finishes and is
- ready to begin your program. It calls your program just like a
- function call:
-
- main( 3,0950 )
-
- Which says we have three pointers beginning at location 0950.
- Then along comes your program:
-
- main( argc, argv )
- int argc; argc is the number of pointers (3).
- char **argv; argv is a pointer (0950) to a pointer (0900)
- to a character 'c'.
- or
-
- char *argv[]; argv is an array of pointers to characters
- beginning at (0950).
-
- I hope this will assist you in visualizing the command line
- process and answer an often asked question.
-