Thread: Help With Parsing String into Array of Strings

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    14

    Question Help With Parsing String into Array of Strings

    Hello, so I am writing a code for an assignment that requires me to break an inputted string into an array of string arguments. Here is my code, which is within the main, to do so. It only puts the first token into my array, and the while loop never goes through a second time. I am wondering if anyone can help me figure out the error in this fragment of code.

    Code:
                char** argsArray = malloc((sizeof(char*)*(numArgs+1)));
                int counter = 0;
                char* next;
                char *delimiters = " \t\n";
                next = strtok(buffer, delimiters);
                while(next != NULL)
                {
                    printf("Does this happen? \n");
                    argsArray[counter] = (char*)malloc((sizeof(char)*strlen(next)));
                    strcpy(argsArray[counter], next);
                    next = strtok(NULL, delimiters);
                    printf("Element %d is: %s \n", counter, argsArray[counter]);
                    counter++;
                }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    What is in "buffer"?

    Also, don't cast the return value of malloc.

    sizeof(char) is and will always be 1 on any architecture. It is mandated by the standard.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    14
    Sorry should have clarified.... buffer is a string that contains the entire user input, meant to be parsed using the delimiters (space, tab, new line).

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Yes I know that, but what does it really contain when you run the program. I.e. what are you testing it with.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    14
    The input I'm testing it with is something like "Hello World" where it should make an array of two strings.... "Hello" and "World" .... but when I test it, it puts "Hello" into element 0 of the array, and then it appears that the token is NULL since it will not execute the loop again at the "World" stage.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    There is another thing that concerns me about your code. You never seem to check whether counter has exceeded the number of char* available in argsArray. In fact, what is the value of numArgs?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    14
    numArgs is never more than 3 when I test it. I have another piece of the program that prints numArgs for me so that I am aware. For example, if I typed "hello world 12" then there are 3 arguments. Spaces are separators for this. My confusion is that I use a very similar method with strtok to count the arguments and even print them one by one (countArgs method shown below).... the problem seems to come with malloc'ing space and putting the strings into my array of char*'s. For whatever reason, after adding the first string to the array, it sees "next" as NULL and thus skips my while loop.

  8. #8
    Registered User
    Join Date
    Jan 2012
    Posts
    14
    Sorry here is the code I mentioned.
    Code:
    int countArgs(char* buffer) {
        char *delimiters = " \t\n";
        int counter = 0;
        char *first = strtok(buffer, delimiters);
        printf("Token %d is %s \n", counter++, first);
        char *next;
        while(next = strtok(NULL, delimiters)) {
            printf("Token %d is %s \n", counter++, next);
        }
        return counter;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing Command Line String Into Array
    By Illusionist15 in forum C Programming
    Replies: 10
    Last Post: 03-31-2011, 09:16 AM
  2. Help parsing equation from string array
    By feanor114 in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2010, 01:30 AM
  3. String parsing(parsing comments out of HTML file)
    By slcjoey in forum C# Programming
    Replies: 0
    Last Post: 07-29-2006, 08:28 PM
  4. basics of array passing and string parsing
    By gregh in forum C Programming
    Replies: 1
    Last Post: 03-01-2002, 07:26 PM