Is there any difference between int main(int argc, char* argv[])
and int main(int argc, char** argv)
I don't see any difference.
And, which is better to use?
Is there any difference between int main(int argc, char* argv[]) and int main(int argc, char** argv)
Asked
Active
Viewed 477 times
1

Anic17
- 712
- 5
- 18
-
1`char* argv[]` and `char **argv` are equivalent. However, the `char** argv[]` you have in your question is not. The choice between the two first ones is arbitrary. I like the brackets because it feels more array-like. – Thomas Jager Jul 14 '20 at 14:19
-
As the startup code does not know which one you are using, the result must be identical with both variants. Therefore you can just pick the one you like most. – Gerhardh Jul 14 '20 at 14:19
-
Technically none. The first (`char *argv[]`) is old convention, and might be considered semantically more accurate since `argv` is an array of pointers. – Some programmer dude Jul 14 '20 at 14:20
-
Typo fixed, I always use `int main(int argc, char* argv[])`, and that's why I have made that typo. – Anic17 Jul 14 '20 at 14:20
-
Personally, I prefer `char **argv` because I think the automatic adjustment of `type[]` to `type*` (for some type `type`) in function parameters leads to confusion for beginners. – Ian Abbott Jul 14 '20 at 14:36
-
This isn't even a case where `main` is special. `void f(int a[])` is just a "fancy" way of writing `void f(int *a)` – ikegami Jul 14 '20 at 14:47
-
1@Some programmer dude, Re "*`argv` is an array of pointers.*", No, it isn't. It's a pointer to one. `sizeof(argv)` will return the size of a pointer, for example. – ikegami Jul 14 '20 at 14:50
3 Answers
5
There is no semantic difference between
int main(int argc, char* argv[])
and
int main(int argc, char** argv)
. They have identical meaning. I personally prefer the former, as I think it more clearly conveys the significance of the second argument.

John Bollinger
- 160,171
- 8
- 81
- 157
1
There is no difference. I prefer the one with the array declaration since it makes it more clear that it is an array of strings.

Alex C
- 217
- 2
- 11
-
2No, `argv` isn't an array of strings. It's a pointer to one. `sizeof(argv)` will return the size of a pointer, for example. – ikegami Jul 14 '20 at 14:52
-
-
1It is not an array of strings in the C sense. It is not a C array at all. It is a C pointer. – ikegami Jul 15 '20 at 19:13
-
@Gerhard, No, `argv` is not an array of any kind. It is a pointer. You made the same mistake as Alex C. /// And "string" is a short hand for "pointer to or array of NUL-terminated sequence of chars". You corrected something that wasn't wrong. /// I didn't say what you claimed I said at all. – ikegami Jul 15 '20 at 20:49
-
@Gerhard , Re "*`argv[argc]` is a null pointer*", Correct. /// Re "*each `argv[n]` will be a pointer to a string.*", Yes, but it's also correct to just say string. /// Re "*argv is the character pointer array*", Incorrect. `argv` is a pointer to the first element of that array. – ikegami Jul 15 '20 at 20:59
-
1@Gerhard, [Proof](https://pastebin.com/6UD92244). You can also prove it by adding `++argv;`. Adding `++array;` would cause the program to fail to compile. But the program still compiles successfully if you add `++pointer;` or `++argv;`. – ikegami Jul 15 '20 at 21:11
-
1@Gerhard, Pointers and arrays are different things. 1) Arrays degrade into pointers to their first element when used as a pointer. So `a == &( a[0] )` will always be true (if `a` is an array). Furthermore, the address of an array is the address of its first element, so `&a == &( a[0] )` is always true. This also means that `a == &a` is always true. 2) You can't assign to an array (which is why `++array;` would fail to compiled in the earlier example), but you can obviously assign to a pointer. (Obviously, you can assign to elements of an array.) – ikegami Jul 15 '20 at 21:27
-
1Finally, 3) Function parameters can't be arrays. `void foo(char x[5]); void bar(char x[]);` means `void foo(char *x); void bar(char *x);`. Arrays used as the argument for parameters will degrade into a pointer as mentioned above. This is why `argv` can't possibly be an array. – ikegami Jul 15 '20 at 21:27
-
1@Gerhard, [Remember](https://stackoverflow.com/q/381542/589924) that `argv[n]` simply a pretty version of `*( argv + n )`. So `argv[n]` works with both arrays and pointers to (elements of) arrays. – ikegami Jul 15 '20 at 21:44
-
0
Both the syntax are same . you can choose any of syntax.
int main(int argc, char* argv[])
int main(int argc, char** argv)
first one is more clear and understandable for beginners.

Sunny Goel
- 1,982
- 2
- 15
- 21
-
1`int b;` and `char x`; also use the same syntax. The fact that they use the same syntax doesn't mean they are interchangeable as you claim. (They do happen to be interchangeable, just not for the reason you claim.) – ikegami Jul 14 '20 at 14:55
-
@ikegami I have not say that you can replace the int with char. int type variable consumes 4 bytes and char consumes 2 bytes. – Sunny Goel Jul 14 '20 at 16:23
-
No, but you did say two things were equivalent because they use the same syntax, which is clearly an invalid argument. You should replace "*Both the syntax are same . you can choose any of syntax.*" with "*Both of the following are equivalent. You can use either one.*" – ikegami Jul 14 '20 at 16:27
-
-
That may have been what you meant, but that's not what you said. If I may be blunt, what you said doesn't make sense, really. (For starters, you said they were the same syntax in the first sentence, and you said they were different syntax in the second.) I proposed a rewording in my previous comment. – ikegami Jul 14 '20 at 16:34
-