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

前端 未结 6 1005
广开言路
广开言路 2020-12-28 17:45

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,

6条回答
  •  既然无缘
    2020-12-28 18:08

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

提交回复
热议问题