question regarding char *argv[]

This is a discussion on question regarding char *argv[] within the C Programming forums, part of the General Programming Boards category; Hi, I have question regarding following example from cprogramming faq section: Code: #include <stdio.h> int main(int argc, char *argv[]) { ...

  1. #1
    cnb
    cnb is offline
    Registered User
    Join Date
    Sep 2008
    Posts
    14

    question regarding char *argv[]

    Hi,
    I have question regarding following example from cprogramming faq section:
    Code:
    #include <stdio.h> 
    
    int main(int argc, char *argv[])
    {
      int i, j;
      for (i = 0; i < argc; i++)
      {
        printf ("argv[&#37;d] is %s\n", i, argv[i]);
          printf ("argv[%d][%d] is %c\n", i, j, argv[i][j]);
      }
      return(0);
    }
    
    /*
     * When invoked with:
     E:\>a.exe -parm1 -a
     * The output is:
      argv[0] is E:\a.exe
      argv[0][0] is E
      argv[0][1] is :
      argv[0][2] is \
      argv[0][3] is a
      argv[0][4] is .
      argv[0][5] is e
      argv[0][6] is x
      argv[0][7] is e
      argv[1] is -parm1
      argv[1][0] is -
      argv[1][1] is p
      argv[1][2] is a
      argv[1][3] is r
      argv[1][4] is m
      argv[1][5] is 1
      argv[2] is -a
      argv[2][0] is -
      argv[2][1] is a
     *
     */
    I have following questions:
    My understanding is that char *argv[] is a array of pointers to string. Is this a two dimensional array? So the *argv[] array contains pointers to string so thats one set of array but i cannot understand how is the second array works?
    Is there a diagram that illustrates the char *argv[]?

    Need to understand the following lines:
    for (j = 0; argv[i][j]; j++)
    printf ("argv[%d][%d] is %c\n", i, j, argv[i][j]);
    In this case the code loops through for a value of i and keeps incrementing value of j. My question is how does it break out of this loop i.e. j=0 and than you increment to j=1 and j=2.....how does it stop this loop? Does it stop after it hits a null or something?

    Thanks
    Last edited by cnb; 10-07-2008 at 12:16 AM.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    argv[] is an array that stores all the arguments. The total number of the arguments are argc. So argv has argc rows. Now, the argument is a string. A string is a char*. Nothing else.
    It is a 2D array, since a pointer is an array in a way. But one dimension is equal to argc, the other doesn't have a specific size, since each argument will differ in size. Some may right it like: int main(int argc, char** argv); which is the same. Generally [] means * in most cases in C.
    The loop seems wrong for me! Are you sure you are not forgetting an inner loop?
    In any case this:
    Code:
      for (i = 0; i < argc; i++)
        printf ("argv[&#37;d] is %s\n", i, argv[i]);
    Will print all the arguments. This:
    Code:
      for (i = 0; i < argc; i++)
          for (j=0; argv[i][j] != \'0'; ++j)
              printf ("argv[%d][%d] is %c\n", i, j, argv[i][j]);
    Would do the same, printing one by one character

    Diagram for char* argv[]. Lets say you put on the command line >>P.exe 12

    [P] [1] [2]
    [. ] [\0] [\0]
    [e]
    [x]
    [e]
    [\0]

    Now, argc will be 2. argv will be the above
    Last edited by C_ntua; 10-07-2008 at 02:25 AM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    Diagram for char* argv[]. Lets say you put on the command line >>P.exe 12

    [P] [1] [2]
    [.] [\0] [\0]
    [e]
    [x]
    [e]
    [\0]

    Now, argc will be 2. argv will be the above
    That looks like a typo, plus argv actually has a terminating null pointer. More accurately:
    [P] [1] [NULL]
    [.] [2]
    [e] [\0]
    [x]
    [e]
    [\0]

    Despite the three elements of argv, argc is indeed 2 in this case.

    Whether argv[0] is "P.exe" or some other representation of the program name is implementation defined (and argv[0] can simply be left as an empty string).
    Last edited by laserlight; 10-07-2008 at 06:19 AM. Reason: Clarification of argv[0].
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    cnb
    cnb is offline
    Registered User
    Join Date
    Sep 2008
    Posts
    14
    Hi,
    Yes there is typo in my original message:
    The code looks like this:
    Code:
    #include <stdio.h> 
    
    int main(int argc, char *argv[])
    {
      int i, j;
      for (i = 0; i < argc; i++)
      {
        printf ("argv[%d] is %s\n", i, argv[i]);
        for (j = 0; argv[i][j]; j++)
          printf ("argv[%d][%d] is %c\n", i, j, argv[i][j]);
      }
      return(0);
    }
    
    /*
     * When invoked with:
     E:\>a.exe -parm1 -a
     * The output is:
      argv[0] is E:\a.exe
      argv[0][0] is E
      argv[0][1] is :
      argv[0][2] is \
      argv[0][3] is a
      argv[0][4] is .
      argv[0][5] is e
      argv[0][6] is x
      argv[0][7] is e
      argv[1] is -parm1
      argv[1][0] is -
      argv[1][1] is p
      argv[1][2] is a
      argv[1][3] is r
      argv[1][4] is m
      argv[1][5] is 1
      argv[2] is -a
      argv[2][0] is -
      argv[2][1] is a
     *
     */
    Yes there is another for loop, but i still do not how this is working i.e. the second for loop:
    The for loop i am talking about is:
    Code:
    for (j = 0; argv[i][j]; j++)
    How does the expression arg[i][j] produces a false which means that you break out of this loop...i.e. what is the false condition.
    This code is provide at the following URL:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    Its the last example on this page that i am looking at.
    Thanks

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Since strings (null terminated char arrays) are passed, when j hits the \0 (the null), it evaluates to false.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    '\0' evaluates to false. argv[i][j] is a nul terminator ('\0') when j is the last element of the array argv[i].

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    Basically, it turns the expression into a boolean expression.
    All expressions can basically be dumbed down to non-0 and 0.
    0 means false, non-0 means true.
    So when argv[i][i] is '\0', it evaluates to 0 which evaluates to false and breaks the loop.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  2. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  3. Replies: 18
    Last Post: 06-21-2003, 10:57 AM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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