Evan Carroll
Evan Carroll

Reputation: 1

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?

Upvotes: 4

Views: 1306

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 477040

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.

Upvotes: 5

Related Questions