Thread: when i would use char *argv[] and when char** argv[] ?

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    92

    Lightbulb when i would use char *argv[] and when char** argv[] ?

    when i would use char *argv[] and when char** argv[] ?

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

    AND

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


    question1 . which one is correct ? what is the difference between them ?


    question 2 say, i am passing command line "this is a test" .....which one i shold use ?

    i have seen both of these two function, so i want to know the difference.

    thanks
    blue_gene

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    correct:
    int main(int argc, char *argv[])

    correct:
    int main(int argc, char **argv)

    incorrect:
    Anything else. (the variable names can be anything you want though). I think there is a third variable sometimes too. Hmm.

    Try this:
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	int i;
    	for (i = 1; i < argc; i++) {
    		printf("%s ", argv[i]);
    	}
    	printf("\n");
    	return 0;
    }
    argc contains the number of arguments (plus 1 for the filename)
    argv[0] is the name of your program. argv[1], argv[2], etc, are the command line arguments you gave.
    Last edited by Brian; 04-14-2004 at 05:30 AM.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    can you give examples when do i use those two ? as i have asked in my question 2
    blue_gene

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Quote Originally Posted by blue_gene
    can you give examples when do i use those two ? as i have asked in my question 2
    example added to my original reply. (and edited again because I made a mistake)
    Last edited by Brian; 04-14-2004 at 05:19 AM.

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    ohh...sorrry, it was not loaded fully that time...let me look at it
    blue_gene

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    i understand char * argv[] as array of strings.

    so argv[0] = first string

    argv[1] = second string

    .....so on


    but what is the meaning of char** argv . is it also array of strings ? how do i use in this case ?
    blue_gene

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >is it also array of strings ?
    Yes.

    > how do i use in this case ?
    The same way as char *argv[].
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    If you want to pass numbers, you are going to have to convert the string to a number. e.g.

    Code:
    #include <stdio.h>
    /* addprog.c - prints the sum of two numbers
        passed as command line arguments */
    
    int main(int argc, char *argv[])
    {
    	int first, last;
    	if (argc != 3) {
    		printf("Usage: addprog <number1> <number2>");
    		
    		getchar();
    		return 0;
    	}
    	
    	first = atoi(argv[1]);  /* Converts string to an integer */
    	last = atoi(argv[2]);
    	
    	printf("%d", first + last);
    	
    	getchar();
    	return 0;
    }

  9. #9
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    in the second syntax , i think i will get an advantage directly as it is 2-d

    so argv[0][0]--->1 st char of 1 string

    argv[0][1]----> ist char of ist string


    like this...


    but if i write using first syntax, then it is difficult to extract chars this way.


    so, when i need chars from the string i should use 2nd syntx.

    and when i need only strings i should use 1st syntx......something like this, i believe
    blue_gene

  10. #10
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Quote Originally Posted by blue_gene
    in the second syntax , i think i will get an advantage directly as it is 2-d

    so argv[0][0]--->1 st char of 1 string

    argv[0][1]----> ist char of ist string


    like this...


    but if i write using first syntax, then it is difficult to extract chars this way.


    so, when i need chars from the string i should use 2nd syntx.

    and when i need only strings i should use 1st syntx......something like this, i believe
    I always use *argv[]. It's the one most people use, and it also has the advantage you said of extracting characters.

    EDIT: no wait you can do argv[x][y] on both types. Silly me.
    Last edited by Brian; 04-14-2004 at 05:37 AM.

  11. #11
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    ok, i see,

    hi brian, i wonder how are making corrections/ changes in the page that you have posted already !!

    i dont see any EDIT button here at reply thread.
    blue_gene

  12. #12
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    ohh..sorry, i got it ..its ok.

    thanks
    blue_gene

  13. #13
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    laffo.

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >in the second syntax , i think i will get an advantage directly as it is 2-d
    There is no difference between the two:
    Code:
    char *argv[]
    and
    Code:
    char **argv
    are equivalent. They both mean the same thing and are used the same way, so neither is advantageous over the other. Use whichever you find to be less confusing.
    My best code is written with the delete key.

  15. #15
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    the title of the original post had this

    char**argv[] !!, that is wrong, that is a 3 dimensional array, not what you want and shouldn't compile. Also, in the first response, someone mentioned a third argument, this is sometimes environment variables, like in linux $(HOME), etc. I can't remember the syntax, I think like this
    char *envp[], right after *argv[].

Popular pages Recent additions subscribe to a feed