Re: g_hash_table problem



Le lun 28/06/2004 à 12:15, Kuang-Chun Cheng a écrit :
> Hi,
> 
> Why the following code not working ?
> The result will be "KEY aaa NOT FOUND".
> 
> The key can't be found if I use buf[], and
> it can be found if I search "aaa" ???
> 
> Thanks.
> 
> 
> ----
> 
> #include <stdio.h>
> #include <stdlib.h>
> #include <unistd.h>
> #include <string.h>
> #include <glib.h>
> 
> int main (int argc, char *argv[])
> {
> char buf[1024];
> GHashTable *htab;
> 
> htab = g_hash_table_new(NULL, NULL);
> 
> g_hash_table_insert(htab, "aaa", NULL);
> 
> strcpy(buf, "aaa");
> if (g_hash_table_lookup(htab, buf)) {
> printf("KEY %s FOUND\n", buf);
> } else {
> printf("KEY %s NOT FOUND\n", buf);
> }
> 
> g_hash_table_destroy(htab);
> 
> exit(0);
> }

As you passed NULL pointers to g_hash_table_new, the comparison is made
on the values ob buf and "aaa" which are their addresses in memory.
That's why buf cannot be found.
Use g_hash_table_new (g_str_hash, g_str_equal); instead. 

Attachment: signature.asc
Description: Ceci est une partie de message =?ISO-8859-1?Q?num=E9riquement?= =?ISO-8859-1?Q?_sign=E9e?=



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]