Re: GList weirdness



> 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?

The "row" variable is being reused and does not contain a copy.  You need to
do something like this:

   gpointer tmp;

   while((row = mysql_fetch_row(result))) {
        printf("fetching %s\n", row[0]);
        tmp = NULL;
        if (row[0] != NULL)
          tmp = (gpointer) strdup(row[0]);
        countries = g_list_append(countries, tmp);
        printf("countries = %s\n", countries->data);
 
   }

Don't forget to deallocate each non-NULL element of "countries."
-----------------------------------------------------------------------------
Mark Leisher                           Rights surrendered are not easily
Computing Research Lab                 regained, and a police state is not
New Mexico State University            worth defending.
Box 30001, Dept. 3CRL                     -- Patrick O'Grady
Las Cruces, NM  88003




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