Thread: argv and argc

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    28

    argv and argc

    Code:
    nt main (int argc, char **argv[]){
    int i=0;
    scanf ("%s",*argv[1]);
    while (argv[1][i]!='\0')
    i++;
    printf ("%d",i);
    }
    i just want to print the number of the digits of the second argument. Why is that wrong??

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well first of all your main declaration is wrong and will probably return a syntax error...
    Code:
    int main (int argc, char *argv[])
    scanf() is a keyboard input function... you push the buttons down and stuff pops up on the screen.
    argv[] is an array of strings taken from the program's command line... scanf() will destroy them.

    if you want the length of a string, there's no reason to reinvent the wheel... just #include <string.h> and use strlen()...
    Code:
    printf("argv[1] is %d characters", strlen(argv[1]));

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by antros48 View Post
    Code:
    nt main (int argc, char **argv[]){
    int i=0;
    scanf ("%s",*argv[1]);
    while (argv[1][i]!='\0')
    i++;
    printf ("%d",i);
    }
    i just want to print the number of the digits of the second argument. Why is that wrong??
    You have the wrong main definition, it should be:
    Code:
    int main(int argc, char *argv[])
    Also you can ditch the scanf function call, it takes input from stdin (keyboard). You already have the first command line argument in argv[1].

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Get rid of the scanf. argv[1] already holds an argument passed into the program from the command line, you don't need to be trying to read into space that already holds valid data. And I believe argv[1] is really the first argument passed into the program, argv[2] is the second.

    All you appear to be doing is counting up until the null terminating character. You could therefore just call strlen on that string and get rid of the loop.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    Thanks a lot guys!! But now i want to check with if -else the second argument string so i can choose what function to call.
    so i did [code]
    if (argv[1][] == "example")
    printf ("example\n");
    [\code]
    but that is wrong again.. Any help about this?

  6. #6
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by antros48 View Post
    Thanks a lot guys!! But now i want to check with if -else the second argument string so i can choose what function to call.
    so i did
    Code:
    if (argv[1][] == "example")
    printf ("example\n");
    but that is wrong again.. Any help about this?
    You cannot compare strings with == in C, you need to resort to the string compare function defined in <string.h>. To compare the first command line argument to 'example' you would do:

    Code:
    if (strcmp(argv[1], "example") == 0) {
        /* Call som function */
    }

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yep... you are working in a language that has *no concept* of text. In C everything is a number and "strings" are just arrays of numbers that represent the characters in the text. Thus... you cannot assign, compare or manipulate strings except through specialized library functions written for the task.

    What you're most likely looking for is strcmp() ... look it up in your C library documentation to get the full informtion.

    Code:
    if ( !strcmp(argv[1],"example"))
      puts(argv[1]);

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    Thanks again!!

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    Another small problem came up..
    Code:
    int main (int argc, char *argv[]){
    char t[4]="new-";
    strcat (t,argv[1]);
    printf("%s",t)
    return 0;
    }
    i want to print new- and the name of argv[i] but i can't tell how much space do i have to keep in array t. I tried to use malloc (strlen argv[i]) =k but an error came up that i cannot insert a variable in the array size.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You only allocated 4 bytes, used them all, leaving no room for even the trailing 0 and then added more...

    try... char t[100];

  11. #11
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    yes i undestand but isn't that waste of space? i mean by using the k = strlen(argv[i]) and then define the size of t to be t[k] can't i use only the space appropriate?

  12. #12
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by antros48 View Post
    yes i undestand but isn't that waste of space? i mean by using the k = strlen(argv[i]) and then define the size of t to be t[k] can't i use only the space appropriate?
    You cannot set or change the size of an array at run-time in ANSI C (thanks for clearing that up MK27) , it must be known at compile-time. You can however use malloc and realloc to obtain and resize chunks of memory dynamically.
    Last edited by iceaway; 09-30-2011 at 08:37 AM.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    If you actually know the appropriate space... yes. But don't forget that when you go strcat() you need the sum of the length of all strings available to you... and you may not know their length at compile time. You can use malloc() of course...

    But I wouldn't be too stingy with space... it's far better to use a bit extra than to not have enough. I have one program here where I malloc 16 megabytes for data manipulation so, in machines with 4 and 8gb of memory, I really wouldn't be too worried about 10 or 20 bytes...

  14. #14
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    Thanks a lot for your help! But will i be very annoying if i asked you iceaway to give me a small example based on my subject???

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by iceaway View Post
    You cannot set or change the size of an array at run-time in C, it must be known at compile-time.
    You can set a variable size array at runtime under the ISO99 standard, but you probably must tell the compiler to do this. You still cannot change the size.

    Quote Originally Posted by antros48 View Post
    an error came up that i cannot insert a variable in the array size.
    What compiler are you using?

    WRT to malloc:

    Code:
    #include <errno.h>
    
    char *t = malloc(strlen(argv[1]));
    if (!t) {
         printf("malloc: %s\n", strerror(errno));
         exit(errno);
    }
    You must free() *t when you are done with it. BTW, "t" is a terrible name for a variable.

    As Tater says, if you are certain the space you need will not exceed a certain maximum, just allocate that. Eg:

    Code:
    char t[4096];
    4 kB is a big string, but very little memory. The OS gives you a fixed size stack anyway -- usually 8+ MB -- so you might as well use it.
    Last edited by MK27; 09-30-2011 at 08:55 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to use argc and argv
    By Salahuddin in forum C Programming
    Replies: 19
    Last Post: 09-09-2011, 03:53 AM
  2. argc--; argv++;
    By C of Green in forum C++ Programming
    Replies: 5
    Last Post: 08-13-2003, 06:16 PM
  3. argc and argv
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2002, 05:21 PM
  4. Argv And Argc
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 12-11-2001, 03:43 PM
  5. more argv and argc
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 09-08-2001, 11:04 PM