Reputation: 8454
Out of the constraints of a coding IDE, I've always written my main function like this:
int main(int argc, char* argv[])
But in IDEs where they start you off with a hello world type application structure to start you off, I've seen it written in different ways. For example in Xcode, it has argv as a constant:
int main(int argc, const char * argv[])
I've also seen people declare argv[] as a double pointer, which I can't understand the reason to:
int main(int argc, char** argv)
Is there any standard or convention to how this is declared? Should I have a double pointer, or a constant?
Bonus question: should the asterisk denoting a pointer be placed just after the data type, just before the variable name or separated in the middle?
Upvotes: 2
Views: 303
Reputation: 42627
I believe the various standards say that argv is NOT const. Now, if your application isn't modifying argv, there's no reason you can't declare it const.
Upvotes: 0
Reputation: 206536
Is there any standard or convention to how this is declared? Should I have a double pointer, or a constant?
This is clearly defined in the C++ Standard.
Reference:
C++03 Section § 3.6.1:
Para 2:
It shall have a return type of int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main:
int main() { /* … */ }
and
int main(int argc, char* argv[]) { /* … */ }
In the latter form argc shall be the number of arguments passed to the program from the environment in which the program is run. If argc is nonzero these arguments shall be supplied inargv[0] through argv[argc-1] as pointers to the initial characters of null-terminated multibyte strings (NTMBSs) (17.3.2.1.3.2) and argv[0] shall be the pointer to the initial character of a NTMBS that represents the name used to invoke the program or "". The value of argc shall be nonnegative. The value of argv[argc] shall be 0. [Note: it is recommended that any further (optional) parameters be added after argv]
Upvotes: 2