The C code
#include <glib.h>
//...
GHashTable *hash = g_hash_table_new(NULL, NULL);
GString val;
g_hash_table_insert(hash, (int*)5, g_string_new("Bar"));
val = g_hash_table_lookup(hash, (int*)5); // line 10
printf("Foo ~ %s\n", val->str); // line 16
if (NULL == val) // line 18
printf("Eet ees null!\n");
Which produced:
ctest.c:10: error: incompatible types in assignment
ctest.c:16: error: invalid type argument of ‘->’ (have ‘GString’)
ctest.c:18: error: invalid operands to binary == (have ‘void *’ and ‘GString’)
What am I doing wrong? 🙁
EDIT: Some might be confused and ask ask themselves “Why didn’t she use g_string_printf()?”
Because I need to access gchar *str, printing was just my way of “debugging” it.
EDIT: Added line 18, and commented the spazzing lines (yes, lots of whites-paces all over the place. I’m dreadful, I know).
The function
g_string_newreturns aGString *. So that’s what’s getting stored in the hash. And the functiong_hash_table_lookupreturns avoid *. You likely want something like this: