How to use g_slist_find_custom()
, when im working with a single list.
And every node of list is a structure.
typedef struct {
int number;
int price;
char* title;
}Books;
GSList *list =NULL,
g_slist_find_custom(list, /*?*/, (GCompareFunc)compp/*?*/);
You can find items in a GSList
using a comparison function:
gint comp(gpointer pa, gpointer pb)
{
const Books *a = pa, *b = pb;
/* Compared by title */
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 */
And you can compare constants using NULL
as second parameter:
gint comp(gpointer p)
{
const Books *x = p;
return strcmp(x->title, "Don Quijote");
}
item = g_slist_find_custom(list, NULL, (GCompareFunc)comp);