C++ - char** argv vs. char* argv[]

后端 未结 7 1188
别跟我提以往
别跟我提以往 2020-12-12 12:09

What is the difference between char** argv and char* argv[]? in int main(int argc, char** argv) and int main(int argc, char* arg

7条回答
  •  一整个雨季
    2020-12-12 12:55

    The bracket form is only useful in statement declarations like:

    char *a[] = {"foo", "bar", "baz"};
    printf("%d\n", sizeof a / sizeof *a);
    // prints 3
    

    Because it knows at compile time the size of the array. When you pass a bracket form as parameter to a function (main or some other one), the compiler has no idea what the size of the array would be at runtime, so it is exactly the same as char **a. I prefer char **argv since it's clearer that sizeof wouldn't work like it would on the statement declaration form.

提交回复
热议问题