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.
>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.