468,484 Members | 1,792 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

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:

Expand|Select|Wrap|Line Numbers
  1. GSList * string_to_GSList(gchar* string){
  2.     char *temp;
  3.     GSList *l = NULL;
  4.  
  5.     temp = strtok(string, "_");
  6.     if(!strcmp(temp, "a")){
  7.         temp = strtok(NULL, "_");
  8.         l = g_slist_append(l, temp);
  9.     }
  10.     else if(!strcmp(temp, "d")){
  11.         temp = strtok(NULL, "_");
  12.         l = g_slist_append(l, GINT_TO_POINTER(atoi(temp)));
  13.     }
  14.     temp = strtok(NULL, "_");
  15.  
  16.     while( temp != NULL){
  17.         if(!strcmp(temp, "a")){
  18.             temp = strtok(NULL, "_");
  19.             l = g_slist_append(l, temp);
  20.  
  21.         }
  22.         else if(!strcmp(temp, "d")){
  23.             temp = strtok(NULL, "_");
  24.             l = g_slist_append(l, GINT_TO_POINTER(atoi(temp)));
  25.         }
  26.         temp = strtok(NULL, "_");
  27.     }
  28.     return l;
  29. }
And somewhere in my main function there is:

Expand|Select|Wrap|Line Numbers
  1. strlist = strdup("a_one_d_1233_a_two_d_244_a_three_d_3_a_four_d_455_  a_five_d_566");
  2. 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.. :)
Mar 11 '08 #1
0 897

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Bruce Davis | last post: by
11 posts views Thread by Kostatus | last post: by
9 posts views Thread by Sudesh Sawant | last post: by
117 posts views Thread by Peter Olcott | last post: by
28 posts views Thread by Jon Davis | last post: by
6 posts views Thread by Ammar | last post: by
2 posts views Thread by Mike Collins | last post: by
2 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.