c - Search g_slist_find_custom() function in GLib -
how use g_slist_find_custom()
, when im working single list. , every node of list 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 gslist
using function compare:
gint comp(gpointer pa, gpointer pb) { const books *a = pa, *b = pb; /* compares title, can compare 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); /* item contains abook */
furthermore can compare constants using null
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);
Comments
Post a Comment