Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   C: problem with GSList (http://www.go4expert.com/forums/c-gslist-t9170/)

miket 7Mar2008 22:37

C: problem with GSList
 
First of all, I'd like to say that I'm fairly new to c programming.

I am trying to write a function which converts a string into a GSList. The string has the following format : "a_stuff_d_123..." where the letters define the type of the GSList element to be created (a for string, d for integer. I only need these two types) and everything after "_" the value. For example, here the first element will be the "stuff" string, the second element will be the integer 123 and so on.
Here is what I've got so far:

Code:

GSList * string_to_GSList(gchar* string){
        char *temp;
        GSList *l = NULL;
       
        temp = strtok(string, "_");
        if(!strcmp(temp, "a")){
                temp = strtok(NULL, "_");
                l = g_slist_append(l, temp);
        }
        else if(!strcmp(temp, "d")){
                temp = strtok(NULL, "_");
                l = g_slist_append(l, GINT_TO_POINTER(atoi(temp)));
        }
        temp = strtok(NULL, "_");

        while( temp != NULL){
                if(!strcmp(temp, "a")){
                        temp = strtok(NULL, "_");
                        l = g_slist_append(l, temp);
                       
                }
                else if(!strcmp(temp, "d")){
                        temp = strtok(NULL, "_");
                        l = g_slist_append(l, GINT_TO_POINTER(atoi(temp)));
                }
                temp = strtok(NULL, "_");
        }
        return l;
}

And somewhere in my main function there is:

Code:

strlist = strdup("a_one_d_1233_a_two_d_244_a_three_d_3_a_four_d_455_a_five_d_566");
list = string_to_GSList(strlist);

Well, the first time I call the fuction it works fine but after the second time using a different string for input it returns the same stuff as in the first call. I understand that I must reset the GSList l every time I call it, that's why I set it to NULL at the beggining.

Any suggestions?
Thanks in advance.. :)


All times are GMT +5.5. The time now is 12:14.