Is char*[] convertible to const char*[]?

Discussion in 'C Programming' started by kevin.hall@motioneng.com, Aug 26, 2005.

  1. Guest

    GCC 3.3 and MSVS 6.0 have no problem converting char*[] to const
    char*[] (not even a warning), but MS's WinCE compiler generated an
    error complained that this was not possible. MS's WinCE compiler did
    allow a conversion from char*[] to const char** though.

    I'm interested in what the standard would say about this. (I don't
    have a copy.)

    Many thanks,

    Kevin
     
    , Aug 26, 2005
    #1
    1. Advertising

  2. Guest

    1. Advertising

  3. Guest

    I know that converting char* to const char* is just fine. The problem
    I'm having is with arrays of strings: "char*[]" to "const char*[]". An
    argument parsing library my company uses specifies "const char*[]"
    because it does not change the command line parameters. But most
    people write main as "int main(int argc, char*argv[])".

    Some compilers convert argc to "const char*[]", but others seem
    incapable. What's correct?
     
    , Aug 27, 2005
    #3
  4. Joe Wright Guest

    wrote:
    > I know that converting char* to const char* is just fine. The problem
    > I'm having is with arrays of strings: "char*[]" to "const char*[]". An
    > argument parsing library my company uses specifies "const char*[]"
    > because it does not change the command line parameters. But most
    > people write main as "int main(int argc, char*argv[])".
    >
    > Some compilers convert argc to "const char*[]", but others seem
    > incapable. What's correct?
    >

    Slow down Kevin, and read what you're writing. Given..

    int main(int argc, char *argv[])

    ...compilers don't convert argc at all. You seem to want to qualify the
    second argument to main as..

    const char *argv[]

    ...but it's not your call. The Standard says all that stuff is writable.
    I think it's probably a bad idea to write to it but it is legal.

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Aug 27, 2005
    #4
  5. Guest

    First of all, thank you guys for trying to help me out. I really do
    appreciate it. I know very well that noone here has to help me and
    that they do it out of their goodwill. Thank you!

    > Slow down Kevin, and read what you're writing.


    I hope I'm coming off quick tempered. I was just trying to clarify
    things. And apparently I still haven't made things completely clear.
    My company's argument parsing library has a function called ParseArgs
    of the form:

    xxx_error_t ParseArgs(int argc, const char*argv[]);

    And from in the main function of our utilities, we write:

    int main(int argc, char* argv[])
    {
    xxx_error_t parse_error = ParseArgs(argc, argv);

    /* ... */
    }

    The copiler error for the WinCE compiler generated an error at the call
    to ParseArgs.

    I hope this clears things up. Thanks again for the help!
     
    , Aug 29, 2005
    #5
  6. Joe Wright wrote:
    > wrote:
    > > I know that converting char* to const char* is just fine. The
    > > problem I'm having is with arrays of strings: "char*[]" to "const
    > > char*[]".


    There is a faq on this.

    > > An argument parsing library my company uses specifies
    > > "const char*[]" because it does not change the command line
    > > parameters. But most people write main as "int main(int argc,
    > > char*argv[])".
    > >
    > > Some compilers convert argc to "const char*[]", but others seem
    > > incapable. What's correct?

    >
    > Slow down Kevin, and read what you're writing. Given..
    >
    > int main(int argc, char *argv[])
    >
    > ..compilers don't convert argc at all. You seem to want to qualify the
    > second argument to main as..
    >
    > const char *argv[]
    >
    > ..but it's not your call. The Standard says all that stuff is writable.


    It depends what you mean by 'all that stuff'. The standard does not
    (itself) give licence for argv[] elements to be writable, just the
    strings which the elements point to. Of course, argv itself can be
    modifed.

    int main(int argc, char **argv)
    {
    if (*argv) **argv = 0; /* fine */
    *argv = 0; /* undefined behaviour */
    argv = 0; /* fine */
    }

    > I think it's probably a bad idea to write to it but it is legal.


    Can anyone name an implementation where they're not writable?

    --
    Peter
     
    Peter Nilsson, Aug 29, 2005
    #6
  7. Guest

    > There is a faq on this.

    Do you mind pointing me to it? The only FAQ item I've ever seen is
    about converting non-arrays -- something like char* to const char*.

    Thanks again! =D
     
    , Aug 29, 2005
    #7
  8. wrote:
    > > There is a faq on this.

    >
    > Do you mind pointing me to it? The only FAQ item I've ever seen is
    > about converting non-arrays -- something like char* to const char*.


    http://www.eskimo.com/~scs/C-faq/q11.10.html

    What you're probably missing is that...

    int main(int argc, char *argv[])

    ....is identical to...

    int main(int argc, char **argv)

    C cannot pass whole arrays, instead it passes pointers to the
    first element. Hence, function parameters declared as arrays
    are silently treated as pointers.

    --
    Peter
     
    Peter Nilsson, Aug 29, 2005
    #8
  9. Guest

    Many thanks for the link! =)
     
    , Aug 31, 2005
    #9
  10. writes:
    > Many thanks for the link! =)


    What??

    Search this newsgroup for "Context, dammit!", and follow the advice.
    (You may have to go back a bit; I suspect most of the recent
    occurrences are reminders like this one.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Aug 31, 2005
    #10
  11. On 26 Aug 2005 14:18:37 -0700, wrote:

    > GCC 3.3 and MSVS 6.0 have no problem converting char*[] to const
    > char*[] (not even a warning), but MS's WinCE compiler generated an
    > error complained that this was not possible. MS's WinCE compiler did
    > allow a conversion from char*[] to const char** though.
    >
    > I'm interested in what the standard would say about this. (I don't
    > have a copy.)


    You can't convert anything to an array in C, ever. (Although in C99 or
    GNUC you can _construct_ an array value with a compound literal.)

    If you're talking about passing arguments to a function parameter
    declared 'const char * []', remember that such a declaration is
    'adjusted' to 'const char * *'. If you try to pass a 'char * []', it
    first decays to 'char * *' and then you are trying to convert that as
    by assignment to 'const char * *' which isn't allowed, and should be
    diagnosed by any compiler. Are you sure you are running each compiler
    in a mode (i.e., with options) that tries to conform to the standard?
    For gcc try -ansi (or -std=c99 if you prefer) and -pedantic and
    preferably -Wall; for MSVC6 /Za /W4, or in the IDE there is a tickbox
    somewhere to "disable language extensions".

    _With a cast_ the pointer conversion is permitted, and should work --
    that is, the conversion itself should work, since the result's target
    must have the same alignment requirement, and same representation so
    accessing the actual char * as a const char * must work. Using _that_
    pointer might be const-unsafe in conjunction with other operations,
    which is one reason the 'outer' conversion is not allowed. (This is
    frequently asked, but not in the FAQ last I looked; 11.10 just says it
    isn't allowed but doesn't explain why.)

    - David.Thompson1 at worldnet.att.net
     
    Dave Thompson, Sep 5, 2005
    #11
    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. AlesD
    Replies:
    3
    Views:
    485
    Victor Bazarov
    Aug 10, 2004
  2. Tom Howard
    Replies:
    2
    Views:
    330
  3. Replies:
    24
    Views:
    854
    Netocrat
    Oct 30, 2005
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,097
    Ian Collins
    May 9, 2006
  5. Javier
    Replies:
    2
    Views:
    585
    James Kanze
    Sep 4, 2007
Loading...

Share This Page