Skip to content

Instantly share code, notes, and snippets.

@danlei
Created July 1, 2010 21:21
Show Gist options
  • Save danlei/460588 to your computer and use it in GitHub Desktop.
Save danlei/460588 to your computer and use it in GitHub Desktop.
g_slist_foreach example
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
typedef struct {
char *name;
} Person;
void print_name(Person *pers) {
puts(pers->name);
}
int main(int argc, char **argv) {
GSList *list = NULL;
Person *pers = NULL;
pers = g_new(Person, 1);
pers->name = "Daniel";
list = g_slist_append(list, pers);
pers = g_new(Person, 1);
pers->name = "Bernd";
list = g_slist_append(list, pers);
g_slist_foreach(list, (GFunc)print_name, NULL);
g_slist_foreach(list, (GFunc)g_free, NULL);
g_slist_free(list);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment