Re: g_hash_table problem



On Mon, 2004-06-28 at 23:05 +0800, Kuang-Chun Cheng wrote:
> Hi Jean,
> 
> It's not working neither :-(  Is this a bug ?

You've also passed NULL as the third parameter to g_hash_table_insert,
so that's exactly what g_hash_table_lookup is returning.  NULL is false.

--
Shaun

> 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. 
> >  
> >
> _______________________________________________
> gnome-devel-list mailing list
> gnome-devel-list gnome org
> http://mail.gnome.org/mailman/listinfo/gnome-devel-list




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