C: problem with GSList

Discussion in 'C' started by miket, Mar 7, 2008.

  1. miket

    miket New Member

    Joined:
    Mar 7, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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.. :)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice