Skip to content

Instantly share code, notes, and snippets.

@danlei
Created July 1, 2010 21:19
Show Gist options
  • Save danlei/460583 to your computer and use it in GitHub Desktop.
Save danlei/460583 to your computer and use it in GitHub Desktop.
gslist.c
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
// gcc -o gslist gslist.c `pkg-config --cflags --libs glib-2.0`
typedef struct {
char *name;
int size;
} Person;
int main(int argc, char **argv) {
GSList *list = NULL;
Person *dan = malloc(sizeof *dan);
dan->name = "Daniel";
dan->size = 179;
list = g_slist_append(list, dan);
Person *fred = g_new(Person, 1);
fred->name = "Fred";
fred->size = 200;
list = g_slist_append(list, fred);
printf("dan's size is %i\n", ((Person *)list->data)->size);
printf("last person's name is %s\n", ((Person *)g_slist_last(list)->data)->name);
g_slist_free(list);
free(dan);
g_free(fred);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment