Thread: Parsing Command Line String Into Array

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    5

    Parsing Command Line String Into Array

    Here's what I want to do:

    Let's say my command line arguments are "123 1fc 234"
    I understand that *argv[1] will contain 123 *argv[2] will contain 1fc etc. (assuming 0 is program name).
    I would like to parse the value in each index of the argv array as a new array containing separate characters. I.e, chararray1[0] = 1, chararray1[1] = 2, chararray1[2] = 3, with a new character array for each command line argument?

    I guess I'm a little confused as to what *argv[1] actually is. Does it contain in itself an array of characters? I've tried setting char *chararray[3] = *argv[x] but I'm getting errors. What is the best way to do this?

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Think of argv as an array of c-strings.
    That would make argv[1] the first arg string ("123")
    And *argv[1] the first character of the first string ('1').
    Try:
    char *chararray = argv[1];
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    Ok that makes sense. Now what if I wanted to make an array of each character array?
    Code:
    char *chararray[ARRAYSIZE];
      
    for(int i = 0; i<argc;i++){
              *chararray[i] = argv[i+1];
              }
    However, I'm getting an error : invalid conversion from *char to char.

  4. #4
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    you should do this:
    Code:
    chararray[i] = argv[i + 1];

  5. #5
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Or even easier:
    Code:
    char** chararray = argv+1;
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    Just spotted this thread and I want to do exactly what the OP wants to do.

    I still can't get it to work though.

    I just made something very unsafe and basic to try it and I get rubbish printed

    Code:
    #include <errno.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
    char *chararray[5];
      
    	for(int i = 0; i<argc;i++)
    	{
    		chararray[i] = argv[i+1];
        }
    		  
    	for(i=0,i<4,i++)
    	{
    		printf("%c",chararray[i]);
    	}
    }

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Crosshash , because they are strings, printf() should use "%s" as formatter.

    I'm still not clear as to what Illusionist15 wanted. An array of each character array sounds like you want a two-dimensional array chararray[MAX_ROWS][MAX_COLS].

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    I made the change as you said and it's still outputting strange characters.

    http://img204.imageshack.us/img204/7048/picxt.png

    What I want - and what I think OP wants, is for example:

    If the argument is

    argument.exe 1234.

    The string, "1234" gets split up and put into a new array, which would be

    chararray[0] = 1
    chararray[1] = 2
    chararray[2] = 3
    chararray[4] = 4

    Then I'd want to turn those into integers.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    #include <errno.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
    char chararray[5][30];    // 5 strings, 30 char each  
    
         for(int i = 0; i < argc; i++)
            strcpy(chararray[i], argv[i+1]);
    You are not allocating any memory for your strings. C does not do it for you!

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Code:
    int len, numbers[100];
    
    if (argc < 2) {
        printf("Missing parmaeter.\n");
        exit(0);
        }
    len = (int)strlen(argv[1]);
    for (int i = 0; i < len; i++) {
        if (isdigit(argv[1][i]))
            numbers[i] = argv[1][i] - '0';
        else {
            printf("Character %d \"%c\" is not a numeric digit.\n", i + 1, argv[1][i]);
            exit(0);
            }
        }
    Last edited by nonoob; 03-31-2011 at 09:00 AM.

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    Quote Originally Posted by nonoob View Post
    Code:
    int len, numbers[100];
    
    if (argc < 2) {
        printf("Missing parmaeter.\n");
        exit(0);
        }
    len = (int)strlen(argv[1]);
    for (int i = 0; i < len; i++) {
        if (isdigit(argv[1][i]))
            numbers[i] = argv[1][i] - '0';
        else {
            printf("Character %d \"%c\" is not a numeric digit.\n", i + 1, argv[1][i]);
            exit(0);
            }
        }
    Excellent, that's exactly what I wanted. Thanks so much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM