0

Normally people write the main function like this:

int main( int argc, char** argv )

However, this came to my mind:

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

or maybe I should write it like this cause it seems more intuitive:

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

What does each of the consts mean in argv (I think I understand them all but am still not sure)?

Also is this a valid code? What issues/limitations can it cause when using argv inside main?

Now what's the difference between this one and the latter:

int main( const int argc, const char* const argv[] )
digito_evo
  • 3,216
  • 2
  • 14
  • 42
  • 1
    Does this answer your question? [what does const mean in c++ in different places](https://stackoverflow.com/questions/10482455/what-does-const-mean-in-c-in-different-places) – Karl Knechtel Jan 31 '22 at 18:21
  • See also: https://isocpp.org/wiki/faq/const-correctness – Karl Knechtel Jan 31 '22 at 18:21
  • 1
    @KarlKnechtel This question may be about what signatures of `main` are allowed and there meaning. – user17732522 Jan 31 '22 at 18:22
  • @user17732522 Yes, actually I have asked two questions here and that's the important one. – digito_evo Jan 31 '22 at 18:24
  • 1
    Only 2 of 4 consts in `int main( const int argc, const char *const *const argv )` work for all compilers. Any compiler can allow the signature though. As for the consts going right to left they prevent you from doing `argv = ...;`, `argv[0] = ...;` and `argv[0][0] = ...;` in `main` respectively. – fabian Jan 31 '22 at 18:27
  • 1
    In that case, how about https://stackoverflow.com/questions/1621574/can-the-arguments-of-mains-signature-in-c-have-the-unsigned-and-const-qualifi ? (Please keep in mind that you are supposed to ask one question at a time.) – Karl Knechtel Jan 31 '22 at 18:28
  • @Karl Knechtel So basically, it's recommended to write `main` like the first one that I have written in the question. Other forms should not be used. Is this correct? – digito_evo Jan 31 '22 at 18:32
  • @fabian Aha, that's what I was looking for. – digito_evo Jan 31 '22 at 18:35

1 Answers1

4

The prototype is defined as "int main( int argc, char** argv )"

There is really no point in using a const pointer to access the parameters later unless you don't want to get it changed, which is up to you

The purpose of const pointers is to make sure that they are not changed throughout the code. You can live without them, but it helps avoid other issues, bugs for example.

On the other side, there is no performance gain (Optimizing_compiler)

1 The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent;9) or in some other implementation-defined manner. 2 If they are declared, the parameters to the main function shall obey the following

constraints: — The value of argc shall be nonnegative. — argv[argc] shall be a null pointer. — If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program startup. The intent is to supply to the program information determined prior to program startup from elsewhere in the hosted environment. If the host environment is not capable of supplying strings with letters in both uppercase and lowercase, the implementation shall ensure that the strings are received in lowercase.

— If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv1 through argv[argc-1] represent the program parameters.

— The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination

so, in short, adding the const is illegal and probably technically not possible (depending on the compiler)

Checking the standard you see the following rules: (ISO/IEC 9899:TC3), go to 5.1.2.2.1

Community
  • 1
  • 1
Marco
  • 1,172
  • 9
  • 24
  • You are quoting the C standard. The question is about C++. "Prototypes" doesn't apply to C++. – user17732522 Jan 31 '22 at 18:36
  • 1
    Presumably there is an item in the C++ standard that defers this issue to the C standard, since that isn't changed in C++ (AFAIK). – Eljay Jan 31 '22 at 18:37
  • 2
    @Eljay It has its own wording. Instead of prototypes mattering it is only important that no `main` overload is pre-defined and of course `void` in the parameter list isn't required: https://eel.is/c++draft/basic.start.main#2. Also the paragraph beforehand needs to differ from C to clarify that `main` is referring to a function in the global namespace. – user17732522 Jan 31 '22 at 18:42
  • 1
    @Eljay: There is an important difference between C and C++ with respect to `main`: In C, it is reentrant. In C++, you cannot call `main`. – MSalters Feb 01 '22 at 10:08