2
votes

In glibc, main is documented as either,

int main (int argc, char *argv[])

Or,

int main (int argc, char *argv[], char *envp[])

Can you define all the arguments as being const if you don't want to change them?

int main (const int argc, const char * const argv[])

Is it supported, unsupported, or illegal?

1
Note that top-level qualifers on function parameter types aren't part of the function declaration, so at least parts of your question aren't relevant. - Kerrek SB
@KerrekSB not irrelevant because I didn't know that. The question is well defined and if they're not part of the function declaration answer and I'll accept. I get typedef errors when arguments are not declared constant and you send them constant pointers, so I figured it was part of the typing. - Evan Carroll
Don't get me wrong, top-level constness still matters. Just not for purposes of prototyping, and hence how you're allowed to define main. Also, the const in "const char*" isn't top-level and matters very much. - Kerrek SB
Are you just curious or are you trying to solve a problem? - klutt

1 Answers

4
votes

In C, the implementation is allowed to support basically any type for the main function, so it may very well be that your particular implementation allows the various forms you have proposed. (And indeed it seems to allow the three-parameter version that exposes the environment.) However, the implementation is only required to accept the two forms

int main(void)

and

int main(int, char**)

Since int(int, const char**) isn't the same type as int(int, char**), your proposed "constified" version is not strictly speaking required to be supported and falls under the first rule. It is, however, quite likely to work since char* and const char* are laid out the same way as far as the ABI is concerned, and the data you're given is mutable anyway.

Note further that int f(int) and int f(const int) are the same identical prototype, so there is no problem here regarding top-level qualifications of the parameters.