qualifying main() parameters

M

Mara Guida

The Standard says that main()
shall be defined with two parameters
int main(int argc, char *argv[]) { /* ... */ }
or equivalent

Does qualifying either of the arguments (or return type) of main()
render the program non-conformant?

1) int main(int argc, const char *argv[]) { /* ... */ }
2) int main(int argc, char ** const argv) { /* ... */ }
3) int main(int argc, char restrict *argv[]) { /* ... */ }
4) int main(volatile int argc, char *argv[]) { /* ... */ }


Or, to put it another way,
are "char **" are "const char **" equivalent when used as function
parameters?
 
D

David RF

The Standard says that main()
shall be defined with two parameters
int main(int argc, char *argv[]) { /* ... */ }
or equivalent

Does qualifying either of the arguments (or return type) of main()
render the program non-conformant?

1) int main(int argc, const char *argv[]) { /* ... */ }
2) int main(int argc, char ** const argv) { /* ... */ }
3) int main(int argc, char restrict *argv[]) { /* ... */ }
4) int main(volatile int argc, char *argv[]) { /* ... */ }

Is not a good idea

5.1.2.2.1

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

Or, to put it another way,
are "char **" are "const char **" equivalent when used as function
parameters?

Yes

int main(int argc, char *argv[]) { /* ... */ }
or equivalent;9)

9) Thus, int can be replaced by a typedef name defined as int, or the
type of argv can be written as
char ** argv, and so on.
 
D

David RF

[...]
are "char **" are "const char **" equivalent when used as function
parameters?

Yes

       int main(int argc, char *argv[]) { /* ... */ }
or equivalent;9)

9) Thus, int can be replaced by a typedef name defined as int, or the
type of argv can be written as
   char ** argv, and so on.

ops, i misread, *char[] and char** are equivalent, char ** and const
char ** doesn't
 
K

Keith Thompson

David RF said:
The Standard says that main()
shall be defined with two parameters
int main(int argc, char *argv[]) { /* ... */ }
or equivalent

Does qualifying either of the arguments (or return type) of main()
render the program non-conformant?

1) int main(int argc, const char *argv[]) { /* ... */ }
2) int main(int argc, char ** const argv) { /* ... */ }
3) int main(int argc, char restrict *argv[]) { /* ... */ }
4) int main(volatile int argc, char *argv[]) { /* ... */ }

Is not a good idea

5.1.2.2.1

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

That doesn't imply that declaring them const is a bad idea.
Assuming it's legal, it merely means that your program promises not
to modify the parameters. Using volatile will probably just inhibit
some optimizations. Using restrict might be problematic, since it
enables optimizations (I haven't thought through the implications).

The question is whether the canonical and const forms are
sufficiently "equivalent" as far as the standard is concerned.
I'm not certain, but I suspect the answer is no. On the other hand,
it's not likely to cause a problem on any real implementations.
On the other other hand, there's not much point in adding qualifiers;
you risk something subtle going wrong without much benefit. (I know
the original question was whether you can, not whether you should.)
 
M

Mara Guida

Is not a good idea

... I suspect the answer is [yes].
... it's not likely to cause a problem ...

Thank you guys.

I will use the Standard sanctioned forms in new code and not complain
as loudly for "revamped" forms in old code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top