Re: g_list behavior: what am I doing wrong?



Ah, I see. Thanks for showing that pitfall, I could easily have done
the same thing.

Cheers,
Chris Anderson

On 7/14/05, Allin Cottrell <cottrell wfu edu> wrote:
On Thu, 14 Jul 2005, Christopher Anderson wrote:

I am curious, could you explain your solution?

OK, at the cost of some embarrassment ;-)

Quoting my test function:

int main (void)
{
      GList *list = NULL;

      list = g_list_append(list, "foo");
      list = g_list_append(list, "bar");
      list = g_list_append(list, "baz");

      puts("\nBefore prepending:");

      while (list) {
        printf("list data = '%s'\n", (char *) list->data);
        list = g_list_next(list);
      }

Bzzzt!!  On the first iteration of the above printing loop I lose
the true start of "list".  I should have used a temporary variable
for looping over the list elements, as in

      GList *tmp = list;

      while (tmp) {
         printf("list data = '%s'\n", (char *) tmp->data);
         tmp = g_list_next(tmp);
      }

Or I suppose I could have used g_list_foreach() with a callback
function, though I find that a bit awkward.

Allin Cottrell





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