GLIB Hash Data

I'm wondering how useful the hashing system can be ibn GLIB

I tried this code, and it seems that instead of copying strings, it just copies the pointer value
resulting in making it impossible to look through input and assigning it to key values, unless I'm
doing something worng.   This is just about useless

#include <stdio.h>
#include <string.h>
#include <glib.h>

GHashTable * hTable;

guint HashFunction(gpointer key){
	char *sKey;
	guint giHashValue = 0;
	int nIndex;
	sKey = key;
	if(key == NULL) return(0);

	for (nIndex = 0; nIndex < strlen(sKey); nIndex++){
		giHashValue = (giHashValue << 4) + (giHashValue ^(guint) sKey[nIndex]);
	return (giHashValue);

gint HashCompare( gpointer sName1, gpointer sName2){
	return (!strcmp((char *)sName1, (char *) sName2));

void print_hash(gpointer key, gpointer value, gpointer otherdata){
	g_print ("Key: %s ==> Value: %s\n", (gchar *) key, (gchar *) value);

int main(int argc, char *argv[]){
	gchar buff_key[255];
	gchar buff_val[255];
	hTable = g_hash_table_new(HashFunction, HashCompare);
		if(scanf("%255s", buff_key) < 1 ) break;
		if(scanf("%255s", buff_val)< 1) break;
		g_print("%s %s\n", buff_key, buff_val);
		g_hash_table_insert(hTable, buff_key, buff_val);

	g_hash_table_foreach(hTable, (GHFunc) print_hash, NULL);


ruben www2:~/gtk > ./hash
One Two
Three Four
Five Six
<CTR d>
Key: Five ==> Value: Six
Key: Five ==> Value: Six
Key: Five ==> Value: Six

If it doesn't work like strcpy, it doesn't do much.


Brooklyn Linux Solutions
__________________________ - Consulting - For the love of Brooklyn - Leadership Development in Free Software - The foundation of Democracy - Unpublished Archive or stories and articles from around the net - Imagine my surprise when I saw you... - See the New Downtown Brooklyn....


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