Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the type of command-line argument `argv` in C?

I'm reading a section from C Primer Plus about command-line argument argv and I'm having difficulty understanding this sentence.

It says that,

The program stores the command line strings in memory and stores the address of each string in an array of pointers. The address of this array is stored in the second argument. By convention, this pointer to pointers is called argv, for argument values .

Does this mean that the command line strings are stored in memory as an array of pointers to array of char?

like image 340
Jin Avatar asked Aug 23 '16 08:08

Jin


People also ask

What is the type of command line arguments in C?

What are Command Line Arguments in C? Command line arguments are nothing but simply arguments that are specified after the name of the program in the system's command line, and these argument values are passed on to your program during program execution.

What type is argv [] in C?

The second parameter, argv (argument vector), is an array of pointers to arrays of character objects. The array objects are null-terminated strings, representing the arguments that were entered on the command line when the program was started.

What is the datatype of first argument in command line arguments?

Always the first command line argument is file path. Only string values can be passed as command line arguments. All the command line arguments are stored in a character pointer array called argv[ ]. Total count of command line arguments including file path argument is stored in a integer parameter called argc.


2 Answers

argv is of type char **. It is not an array. It is a pointer to pointer to char. Command line arguments are stored in the memory and the address of each of the memory location is stored in an array. This array is an array of pointers to char. argv points to first element of this array.

                  Some
                  array

                 +-------+        +------+------+-------------+------+
argv ----------> |       |        |      |      |             |      |
                 | 0x100 +------> |      |      | . . . . . . |      |  Program Name1
         0x900   |       |        |      |      |             |      |
                 |       |        +------+------+-------------+------+
                 +-------+         0x100  0x101
                 |       |        +------+------+-------------+------+
                 | 0x205 |        |      |      |             |      |
         0x904   |       +------> |      |      | . . . . . . |      |  Arg1
                 |       |  .     |      |      |             |      |
                 +-------+        +------+------+-------------+------+
                 |  .    |  .      0x205  0x206
                 |  .    |
                 |  .    |  .
                 |  .    |
                 +-------+  .     +------+------+-------------+------+
                 |       |        |      |      |             |      |
                 | 0x501 +------> |      |      | . . . . . . |      |  Argargc-1
                 |       |        |      |      |             |      |
                 +-------+        +------+------+-------------+------+
                 |       |         0x501  0x502
                 | NULL  |
                 |       |
                 +-------+


0xXXX Represents memory address

1. In most of the cases argv[0] represents the program name but if program name is not available from the host environment then argv[0][0] represents null character.

like image 149
haccks Avatar answered Nov 10 '22 12:11

haccks


Directly quoting from C11, chapter §5.1.2.2.1/p2, program startup, (emphasis mine)

int main(int argc, char *argv[]) { /* ... */ }

[...] If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, [...]

and

[...] and the strings pointed to by the argv array [...]

So, basically, argv is a pointer to the first element of an array of strings note. This can be made clearer from the alternative form,

int main(int argc, char **argv) { /* ... */ }

You can rephrase that as pointer to the first element of an array of pointers to the first element of null-terminated char arrays, but I'd prefer to stick to strings .


NOTE:

To clarify the usage of "pointer to the first element of an array" in above answer, following §6.3.2.1/p3

Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. [...]

like image 35
Sourav Ghosh Avatar answered Nov 10 '22 11:11

Sourav Ghosh