您可以使用函数比较在GSList
项目:
gint comp(gpointer p)
{
const Books *x = p;
return strcmp(x->title, "Don Quijote");
}
item = g_slist_find_custom(list, NULL, (GCompareFunc)comp);
:
gint comp(gpointer pa, gpointer pb)
{
const Books *a = pa, *b = pb;
/* Compares by title, but also you can compare by index or price */
return strcmp(a->title, b->title);
}
GSList *list = NULL;
Books temp, *item, *abook = g_malloc(sizeof(Books));
strcpy(abook->title, "Don Quijote");
list = g_slist_append(list, abook);
/* more code */
temp.title = "Don Quijote";
item = g_slist_find_custom(list, &temp, (GCompareFunc)comp);
/* now item contains abook */
此外,您可以使用NULL
作为第二个参数比较常数