Re: passing glist to func



revans wrote:

Please forgive my ignorance, but could someone explain why this doesn't
work? I want to pass a pointer to a glist to a function and modify that
glist there. The following program prints the value of "a" in function foo
then gives a Segmentation fault trying to print the value of "list" in
main.

I am by no means a C expert but I would suggest this version:

void foo(GList **a)
{
  *a=g_list_append(*a, "text");
  *a=g_list_first(*a); /* this should be unnecessary */
  printf("*a=%s\n", (char *) (*a)->data);
}

int main()
{
  GList *list=NULL;
  foo(&list);
  list=g_list_first(list);
  printf("list=%s\n", (char *) list->data);
  return 0; /* remember to free the items if you put anything else than constants in it */
}

Regards,
WL




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