Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it valid to use const char*[] as the type of second parameter of main

I am learning C++ and learnt that the following given declarations are equivalent:

int main (int argc, char *argv[]); //first declaration
int main (int argc, char **argv);  //RE-DECLARATION. Equivalent to the above declaration

My question is that if i change the declaration to say:

//note the added const
int main (int argc,const char *argv[]); //IS THIS VALID? 

Is the above declaration where i have added a const valid? That is, does the C++ standard allow this modified declaration with the added const.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Adding const changes the type of the function. It is not declaration of the same function.

Whether that is valid for main in particular, depends on language implementation:

[basic.start.main]

An implementation shall not predefine the main function.
Its type shall have C++ language linkage and it shall have a declared return type of type int, but otherwise its type is implementation-defined.
An implementation shall allow both

  • a function of () returning int and
  • a function of (int, pointer to pointer to char) returning int

as the type of main ([dcl.fct]).

It may in theory be valid in some implementation, but it will not be portable to all systems.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading