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

后端 未结 7 1163
别跟我提以往
别跟我提以往 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:35

    They are entirely equivalent. char *argv[] must be read as array of pointers to char and an array argument is demoted to a pointer, so pointer to pointer to char, or char **.

    This is the same in C.

    0 讨论(0)
  • 2020-12-12 12:38

    They are indeed exactly the same.

    The golden rule of arrays to remember is:

    "The name of an array is a pointer to the first element of the array."

    So if you declare the following:

    char text[] = "A string of characters.";

    Then the variable "text" is a pointer to the first char in the array of chars you've just declared. In other words, "text" is of type char *. When you access an element of an array using [index], what you're actually doing is adding an offset of index to the pointer to the first element of the array, and then dereferencing this new pointer. The following two lines will therefore initialize both variables to 't':

    char thirdChar = text[3];
    char thirdChar2 = *(text+3);
    

    Using the square brackets is a convience provided by the language that makes the code much more readable. But the way this works is very important when you start thinking about more complex things, such as pointers to pointers. char** argv is the same as char* argv[] because in the second case "the name of the array is a pointer to the first element in the array".

    From this you should also be able to see why it is that array indexes start from 0. The pointer to the first element is the array's variable name (golden rule again) plus an offset of... nothing!

    I've had debates with a friend of mine as to which is better to use here. With the char* argv[] notation it may be clearer to the reader that this is in fact an "array of pointers to characters" as opposed to the char** argv notation which can be read as a "pointer to a pointer to a character". My opinion is that this latter notation doesn't convey as much information to the reader.

    It's good to know that they're exactly the same, but for readablity I think that if the intention is an array of pointers then the char* argv[] notation conveys this much more clearly.

    0 讨论(0)
  • 2020-12-12 12:53

    For all practical purposes, they're the same. This is due to C/C++'s handling of arrays passed as arguments, where an array decays to a pointer.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-12 12:56

    Both are same for your usage except for the following subtle differences:

    • Sizeof will give different results for both
    • Also second one may not be reassigned to new memory area since it's an array
    • With second one you can use only those indexes which are valid. It's unspecified by C/C++ if you try to use an array index beyond array length. However with char** you can use any index from 0 to ...
    • Second form can only be used as formal parameters to a function. While first can even be used to declare variables within a stack.
    0 讨论(0)
  • 2020-12-12 12:57

    For the first part of the question:

    • char** argv: pointer to a pointer to a char
    • char* argv[]: pointer to an array

    So the question is whether a pointer to a type C and an array C[] are the same things. They are not at all in general, BUT they are equivalent when used in signatures.

    In other words, there is no difference in your example, but it is important to keep in mind the difference between pointer and array otherwise.

    0 讨论(0)
提交回复
热议问题