Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > main() again, and const

Reply
Thread Tools

main() again, and const

 
 
ec429
Guest
Posts: n/a
 
      01-08-2011
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
--
'sane', adj.: see 'unimaginative'
on the web - http://jttlov.no-ip.org
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      01-09-2011
ec429 <> writes:

> 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);
}

--
Ben.
 
Reply With Quote
 
 
 
 
ec429
Guest
Posts: n/a
 
      01-09-2011
On 09/01/11 03:29, Ben Bacarisse wrote:
> ec429<> writes:
>
>> 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
--
'sane', adj.: see 'unimaginative'
on the web - http://jttlov.no-ip.org
 
Reply With Quote
 
Uno
Guest
Posts: n/a
 
      01-09-2011
On 1/8/2011 9:01 PM, ec429 wrote:
> On 09/01/11 03:29, Ben Bacarisse wrote:
>> ec429<> writes:
>>
>>> 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?
--
Uno
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<const MyType> Vs const vector<MyType> magnus.moraberg@gmail.com C++ 2 02-09-2009 10:45 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments