Re: g_hash_table problem



Hi Jean,

It's not working neither :-(  Is this a bug ?

Regards
KC


Jean Bréfort wrote:

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.



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