const char* argv

Discussion in 'C++' started by JKop, Sep 30, 2004.

  1. JKop

    JKop Guest

    Why isn't:

    int main(int argc, char* argv[]) { /* ... */ }


    as:


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


    I assume you can't edit the strings which argv points to. . . right ?


    -JKop
     
    JKop, Sep 30, 2004
    #1
    1. Advertising

  2. JKop

    Sharad Kala Guest

    "JKop" <> wrote in message >
    > Why isn't:
    >
    > int main(int argc, char* argv[]) { /* ... */ }
    >
    >
    > as:
    >
    >
    > int main(int argc, const char* argv[])
    >
    >
    > I assume you can't edit the strings which argv points to. . . right ?


    No. This is a quote from C99 -
    "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."

    Sharad
     
    Sharad Kala, Sep 30, 2004
    #2
    1. Advertising

  3. JKop

    JKop Guest


    > No. This is a quote from C99 -
    > "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."



    This is a genuine question: What has that to do with C++? I take it that C99
    is some sort of... other dialect of C or C++ . . . ?

    -JKop
     
    JKop, Sep 30, 2004
    #3
  4. JKop

    Sharad Kala Guest

    "JKop" <> wrote in message
    >
    > > No. This is a quote from C99 -
    > > "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."

    >
    >
    > This is a genuine question: What has that to do with C++? I take it that

    C99
    > is some sort of... other dialect of C or C++ . . . ?


    Nah...C++ is based on C90 actually. You can find references to C subclauses
    in the C++ Standard. C99 is the more recent revision of the C Standard, I
    made a quote from that. AFAIK C++ inherits it the same way as C does in this
    regard, not very sure though.

    Sharad
     
    Sharad Kala, Sep 30, 2004
    #4
  5. JKop wrote:
    >
    > > No. This is a quote from C99 -
    > > "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."

    >
    > This is a genuine question: What has that to do with C++? I take it that C99
    > is some sort of... other dialect of C or C++ . . . ?


    It is the 1999 version of the C standard.

    Hmm. The C++ standard doesn't seem to say anything about it. But since
    this mechanism is pretty much equivalent in C++ to the C mechanism, I guess
    that it is the same in C++.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Sep 30, 2004
    #5
  6. JKop

    Mike Wahler Guest

    "JKop" <> wrote in message
    news:59T6d.32502$...
    >
    > Why isn't:
    >
    > int main(int argc, char* argv[]) { /* ... */ }
    >
    >
    > as:
    >
    >
    > int main(int argc, const char* argv[])


    Because the 'argv[]' strings are modifiable.

    >
    >
    > I assume you can't edit the strings which argv points to. . . right ?


    Wrong.

    -Mike
     
    Mike Wahler, Sep 30, 2004
    #6
  7. In article <5XV6d.2842$>,
    says...

    > > I assume you can't edit the strings which argv points to. . . right ?

    >
    > Wrong.
    >

    I would say, this depends on the Startup-Code. Especially, when you're
    on an embedded system.
     
    Michael Bruschkewitz, Oct 1, 2004
    #7
  8. JKop wrote:
    > Why isn't:
    >
    > int main(int argc, char* argv[]) { /* ... */ }
    >
    >
    > as:
    >
    >
    > int main(int argc, const char* argv[])
    >
    >
    > I assume you can't edit the strings which argv points to. . . right ?


    C++98 retains C90 as a subset except of the case where otherwise is
    mentioned.

    Now I think that the C90 text should be included in C++98, but that's
    another issue.


    In C90 it is mentioned:


    "Program startup"

    The function called at program startup is named main . The
    implementation declares no prototype for this function. It can be
    defined with no parameters:

    int main(void) { /*...*/ }

    or with two parameters (referred to here as argc and argv , though any
    names may be used, as they are local to the function in which they are
    declared):

    int main(int argc, char *argv[]) { /*...*/ }


    If they are defined, the parameters to the main function shall obey
    the following constraints:

    * The value of argc shall be nonnegative.

    * argv[argc] shall be a null pointer.

    * If the value of argc is greater than zero, the array members
    argv[0] through argv[argc-1] inclusive shall contain pointers to
    strings, which are given implementation-defined values by the host
    environment prior to program startup. The intent is to supply to the
    program information determined prior to program startup from elsewhere
    in the hosted environment. If the host environment is not capable of
    supplying strings with letters in both upper-case and lower-case, the
    implementation shall ensure that the strings are received in
    lower-case.

    * If the value of argc is greater than zero, the string pointed to by
    argv[0] represents the program name ;argv[0][0] shall be the null
    character if the program name is not available from the host
    environment. If the value of argc is greater than one, the strings
    pointed to by argv[1] through argv[argc-1] represent the program
    parameters .

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



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Oct 2, 2004
    #8
  9. JKop

    Mike Wahler Guest

    "Michael Bruschkewitz" <> wrote in message
    news:...
    > In article <5XV6d.2842$>,
    > says...
    >
    > > > I assume you can't edit the strings which argv points to. . . right ?

    > >
    > > Wrong.
    > >

    > I would say, this depends on the Startup-Code. Especially, when you're
    > on an embedded system.


    Well, if the implementation for the embedded system is not
    a hosted implementation (imo typical of an embedded implementation),
    yes, it depends. On such a system, A 'main()' is not required
    at all. So any requirements about 'main()' refer to a hosted
    implementation.

    -Mike
     
    Mike Wahler, Oct 2, 2004
    #9
  10. In article <sbo7d.683$>,
    says...
    > Well, if the implementation for the embedded system is not
    > a hosted implementation (imo typical of an embedded implementation),
    > yes, it depends. On such a system, A 'main()' is not required
    > at all. So any requirements about 'main()' refer to a hosted
    > implementation.

    I never needed main on the embedded system, it always used other ways to
    find the startup function(s). But it would be possible.
    I've my copy of the standard not at hand, so I currently don't know what
    the standard says.
    However, I don't think it is very recommendable to modify the strings
    from argv, or the pointers inside argv. To be safe, you can't increase
    the amount of memory needed for the pointers nor the strings. So, the
    modification is limited to the cases where the number of the strings or
    the length of the strings decreases.
    I would avoid such potential pitfalls by a clear solution without
    modifying argv. For example, convert argc,argv to
    std::vector<std::string> and modify this. If you're allowed to use STL.
    Especially, because startup is not a performance issue.
    Also, it will always be possible to assign a new value to argv itself.

    Regards,
    Michael B.
     
    Michael Bruschkewitz, Oct 2, 2004
    #10
  11. JKop

    JKop Guest


    > In C90 it is mentioned:



    It's in the C++ Standard also.


    -JKop
     
    JKop, Oct 2, 2004
    #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. Bret

    char **argv vs. char* argv[]

    Bret, Aug 31, 2003, in forum: C Programming
    Replies:
    21
    Views:
    4,706
    Richard Heathfield
    Sep 3, 2003
  2. David
    Replies:
    10
    Views:
    6,078
    Richard Heathfield
    Sep 15, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,716
    Old Wolf
    Jan 20, 2004
  4. jab3

    char **argv & char *argv[]

    jab3, Dec 4, 2004, in forum: C Programming
    Replies:
    5
    Views:
    722
    Chris Torek
    Dec 8, 2004
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,154
    Ian Collins
    May 9, 2006
Loading...

Share This Page