main() again, and const

E

ec429

Niggly standards question, it's just been bugging me:
Of course we all know that main's declaration should be either
int main(void)
or
int main(int argc, char *argv[])
BUT... is it legal to use
int main(int argc, const char *argv[])
instead? The FAQ (11.12a) doesn't mention qualifiers at all, only 'char
** argv', the irrelevance of the names, and old-style syntax. Having
inspected the C99 standard (N1124), I see (from 5.1.2.2.1p1) that the
parameters need only be "equivalent", with a footnote that "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."
However, I cannot find a definition in the standard of 'equivalent'
types, only of compatible types (6.2.7). Of course it is legal to call
a function taking a 'const char *' and pass it a 'char *', because
const-ness can always be added.
Logically, then, it /ought/ to be legal, but the standard isn't clear
enough for me to be sure. Also, if it is legal, it seems like a good
thing to do in most cases (as a program is unlikely to need to modify
its argv) if one is trying to make full use of _const_.
So, is it legal or not?
-Edward
 
B

Ben Bacarisse

ec429 said:
Niggly standards question, it's just been bugging me:
Of course we all know that main's declaration should be either
int main(void)
or
int main(int argc, char *argv[])
BUT... is it legal to use
int main(int argc, const char *argv[])
instead?

Almost certainly not, though I can't see why you'd want to do this.
The FAQ (11.12a) doesn't mention qualifiers at all, only
char ** argv', the irrelevance of the names, and old-style syntax.
Having inspected the C99 standard (N1124), I see (from 5.1.2.2.1p1)
that the parameters need only be "equivalent", with a footnote that
"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."
However, I cannot find a definition in the standard of 'equivalent'
types, only of compatible types (6.2.7).

It's a shame that "equivalent" is not defined but it is unlikely that it
is intended to include incompatible types, and const char ** and char **
are incompatible. Worse, they are not even "assignment compatible" by
which I mean they don't satisfy the weaker requirement imposed on
assignments or, by extension, arguments passed to functions
Of course it is legal to
call a function taking a 'const char *' and pass it a 'char *',
because const-ness can always be added.

.... but not at the extra level of remove introduced by the extra
pointer. You can't pass a value of type char ** to a function that
takes a const char **.
Logically, then, it /ought/ to be legal, but the standard isn't clear
enough for me to be sure. Also, if it is legal, it seems like a good
thing to do in most cases (as a program is unlikely to need to modify
its argv) if one is trying to make full use of _const_.

If you feel you need to do this you can always write this:

int main(int argc, char *argv[])
{
return my_main(argc, (const char **)argv);
}
 
E

ec429

ec429 said:
Niggly standards question, it's just been bugging me:
Of course we all know that main's declaration should be either
int main(void)
or
int main(int argc, char *argv[])
BUT... is it legal to use
int main(int argc, const char *argv[])
instead?

Almost certainly not, though I can't see why you'd want to do this.
Of course it is legal to
call a function taking a 'const char *' and pass it a 'char *',
because const-ness can always be added.

.... but not at the extra level of remove introduced by the extra
pointer. You can't pass a value of type char ** to a function that
takes a const char **.
You are, of course, right - because I tend to use the char *[] form, I'd
forgotten that it's actually a pointer to a pointer.
Thanks!
-Edward
 
U

Uno

ec429 said:
Niggly standards question, it's just been bugging me:
Of course we all know that main's declaration should be either
int main(void)
or
int main(int argc, char *argv[])
BUT... is it legal to use
int main(int argc, const char *argv[])
instead?

Almost certainly not, though I can't see why you'd want to do this.
Of course it is legal to
call a function taking a 'const char *' and pass it a 'char *',
because const-ness can always be added.

.... but not at the extra level of remove introduced by the extra
pointer. You can't pass a value of type char ** to a function that
takes a const char **.
You are, of course, right - because I tend to use the char *[] form, I'd
forgotten that it's actually a pointer to a pointer.

When would you be making your second main call?
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top