Hash table problems, again


I've already asked about this a while ago, but didn't get any
reactions. I'm still stuck on this, so I'm trying again ...

I can't quite figure out why the attached progrem doesn't just
store and retrieve the strings "0" - "19" like it should. Can
anybodypoint out to me what I'm doing wrong? It seems that the
hash table does not copy key values, so giving each item a new
string seems only appropriate but I'm still missing something.

It's probably something simple, but I can't seem to see it (maybe
I've just been looking at the same piece of code for too long) ... If
anybody out there has used hash tables, I'd apprecate a word
of advice.

Thanks a lot for any help
--> Robert

----------------------------------- cut here ------------------------------
#include "glib.h"
#include <stdio.h>

guint TFWhash (gconstpointer key);
gint TFWhashcmp (gconstpointer a, gconstpointer b);

int main (int argc, char **argv)
     int       i;
     char      *s;
     GHashTable     *h;

     h = g_hash_table_new (TFWhash, TFWhashcmp);
     for (i=0; i<20; i++)
          s = new char[80];

          sprintf (s, "%d", i);
          g_hash_table_insert (h, s, s);

     s = new char[80];
     for (i=0; i<20; i++)
          char      *v;

          sprintf (s, "%d", i);
          v = (char *) g_hash_table_lookup (h, s);
          printf ("%s\n", v);

guint TFWhash (gconstpointer key)
        char    *s = (char *)key;
        int     i, v=0,

        for (i=0; i<l; i++)
                v = (64*v + s[i])%101;

        return (v);

gint TFWhashcmp (gconstpointer a, gconstpointer b)
        return (strcmp((char *)a,(char *)b));

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