main() again, and const

Discussion in 'C Programming' started by ec429, Jan 8, 2011.

  1. ec429

    ec429 Guest

    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
    ec429, Jan 8, 2011
    #1
    1. Advertising

  2. 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.
    Ben Bacarisse, Jan 9, 2011
    #2
    1. Advertising

  3. ec429

    ec429 Guest

    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
    ec429, Jan 9, 2011
    #3
  4. ec429

    Uno Guest

    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
    Uno, Jan 9, 2011
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    11
    Views:
    1,099
  2. Javier
    Replies:
    2
    Views:
    559
    James Kanze
    Sep 4, 2007
  3. 0m
    Replies:
    26
    Views:
    1,111
    Tim Rentsch
    Nov 10, 2008
  4. fungus
    Replies:
    13
    Views:
    886
    fungus
    Oct 31, 2008
  5. Replies:
    2
    Views:
    539
    Andrew Koenig
    Feb 9, 2009
Loading...

Share This Page