Created
January 15, 2024 08:09
-
-
Save garlic0x1/208d1f4203c822b86a06c0d0c99af8e1 to your computer and use it in GitHub Desktop.
GLib collections examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <glib.h> | |
int main(int argc, char *argv[]) | |
{ | |
/* Linked List demo */ | |
GList *list = NULL; | |
list = g_list_append(list, "Hello world!"); | |
list = g_list_append(list, "Bye world!"); | |
printf("first: %s\n", (char *)g_list_first(list)->data); | |
printf("length: %d\n", g_list_length(list)); | |
g_list_free(list); | |
/* end */ | |
/* Hash Table demo */ | |
GHashTable *dict = g_hash_table_new(g_str_hash, g_str_equal); | |
g_hash_table_insert(dict, "hi", "world"); | |
g_hash_table_insert(dict, "key", "value"); | |
printf("key: %s\n", (char *)g_hash_table_lookup(dict, "key")); | |
printf("hi: %s\n", (char *)g_hash_table_lookup(dict, "hi")); | |
g_hash_table_destroy(dict); | |
/* end */ | |
/* Array demo */ | |
GArray *int_vec = g_array_new(FALSE, FALSE, sizeof(int)); | |
int v1 = 2, v2 = 3, v3 = 45; | |
g_array_append_val(int_vec, v1); | |
g_array_append_val(int_vec, v2); | |
g_array_append_val(int_vec, v3); | |
printf("int_vec[0]: %d\n", g_array_index(int_vec, int, 0)); | |
printf("int_vec[1]: %d\n", g_array_index(int_vec, int, 1)); | |
printf("int_vec[2]: %d\n", g_array_index(int_vec, int, 2)); | |
g_array_free(int_vec, TRUE); | |
/* end */ | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment