Re: GList weirdness


"HaB JacKaL" <feelthel0ve hotmail com> writes:

> GList *countries = NULL;
> .
> .
> .
> while((row = mysql_fetch_row(result)))
> 	{
> 	  printf("fetching %s\n", row[0]);
> 	  countries = g_list_append(countries, (gpointer)row[0]);
> 	  printf("countries = %s\n", countries->data);
> 	}
>       printf("outside while countries = %s\n", countries->data);
>       combo = gtk_combo_new();
>       gtk_combo_set_popdown_strings(GTK_COMBO(combo), countries);
> .
> .
> .
> The out put is as follows:
> fetching United States
> countries = United States
> fetching Canada
> countries = Canada
> outside while countries =
> I have NO CLUE why the values are disappearing outside of that while
> loop.  The declaration for countries is inside the same func.  Anyone
> know?

most probably because you didn't fflush(stdout). I suggest you use 
g_print() or g_message() for debugging output which flushes the output
for you.

However if this really is the code you are executing to get this output,
something is wrong. Since you do g_list_append, the output should look

 fetching United States
 countries = United States
 fetching Canada
 countries = United States

since you are always printing the value of the first list element. 
You want to use g_list_prepend() here simply for performance reasons
(appending to a GList is a very inefficient operation compared to 
prepending). If you need the list the other way around, call 
g_list_reverse() once you have set up the list.

Salut, Sven

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]