Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining main with constant arguments (const int argc, const char * const argv[])?

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?

like image 280
NO WAR WITH RUSSIA Avatar asked Oct 23 '25 16:10

NO WAR WITH RUSSIA


1 Answers

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.

like image 111
Kerrek SB Avatar answered Oct 25 '25 05:10

Kerrek SB