Is argv[] terminated by a NULL string?

This is a discussion on Is argv[] terminated by a NULL string? within the C Programming forums, part of the General Programming Boards category; Is it true that argv[] is terminated by a NULL string? Like Code: Arguments: argv[0] = "program" argv[1] = "arg1" ...

  1. #1
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046

    Is argv[] terminated by a NULL string?

    Is it true that argv[] is terminated by a NULL string? Like
    Code:
    Arguments:
    
    argv[0] = "program"
    argv[1] = "arg1"
    argv[2] = "arg2"
    argv[3] = ""
    So can you go
    Code:
    int x;
    for(x = 0; argv[0][0]; x ++) {
        /* ...code...*/
    }
    Instead of

    Code:
    int x;
    for(x = 0; x < argc; x ++) {
        /* ...code... */
    }
    Because then I can pass one less parameter to my argument validating function(s).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int x;
    for(x = 0; argv[0][0]; x ++) {
        /* ...code...*/
    }
    Um, no. I think you mean:
    Code:
    #include<stdio.h>
    int main( int argc, char *argv[] )
    {
        size_t x;
    
        for( x = 0; argv[ x ]; x++ )
            printf("argv[ %d ] is %s\n", x, argv[ x ] );
    
        return 0;
    }
    Otherwise, all you're doing is testing the first argument's first character over and over and over. (Indefinately.) Then X overflows itself, and starts to wrap around, which really has no effect, other than to keep on going forever and ever and...


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Sorry, a typo . . . but you're wrong too.

    Code:
    #include<stdio.h>
    int main( int argc, char *argv[] )
    {
        size_t x;
    
        for( x = 0; *argv[ x ]; x++ )  /* need a * here */
            printf("argv[ %d ] is %s\n", x, argv[ x ] );
    
        return 0;
    }
    Otherwise you're testing the address of the current string.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Run it. But to answer your question, no there is not an extra string of nothingness.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    argv[argc] is NULL, not ""
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Oh, sorry quzah, I thought argv[argc] was "".

    But is argv[argc] guarenteed to be NULL? Like, is it part of the standard?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by dwks
    But is argv[argc] guarenteed to be NULL? Like, is it part of the standard?
    Yes.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Thanks for the clarification.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Jul 2005
    Posts
    3
    Quote Originally Posted by dwks
    Sorry, a typo . . . but you're wrong too.

    Code:
    #include<stdio.h>
    int main( int argc, char *argv[] )
    {
        size_t x;
    
        for( x = 0; *argv[ x ]; x++ )  /* need a * here */
            printf("argv[ %d ] is %s\n", x, argv[ x ] );
    
        return 0;
    }
    Otherwise you're testing the address of the current string.

    Should be for( x = 0; argv[ x ]; x++ ) , not for( x = 0; *argv[ x ]; x++ ) . otherwise it will cause a segmentation fault.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    That's already been pointed out. I thought argv[argc] was "", but in reality it is NULL. The * would be required if argv[argc] was "".
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  2. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21