Jin Jin - 1 year ago 73
C Question

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

I'm reading a section from C Primer Plus about command-line argument

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
, for argument values .

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

Answer Source

Directly quoting from C11, chapter §, 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 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 .


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

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. [...]

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download