2 u010559765 u010559765 于 2014.07.25 11:03 提问

g_hash_table_insert函数无法正确插入

如题:g_hash_table_insert函数在循环中时,把循环变量作为键的时候无法正确插入,例子是插入10值结果只得到八个。
#include
#include
#include
#include
int main(int argc, char** argv) {
int a,b,c,d;
b=2;
char outbuf[100]="Columbus";
int i=0;
GHashTable* hash = g_hash_table_new(g_int_hash, g_int_equal);
for(i=0;i<10;i++){
g_hash_table_insert(hash, &i,outbuf);
}
g_printf("There are %d keys in the hash\n", g_hash_table_size(hash));
for(i=0;i<10;i++){
g_printf("The %d is %s\n",i, g_hash_table_lookup(hash, &i));
}
g_printf("The 2 is %s\n", g_hash_table_lookup(hash, &b));
g_hash_table_destroy(hash);
return 0;
}

输出的结果是:
There are 8 keys in the hash
The 0 is Columbus
The 1 is Columbus
The 2 is Columbus
The 3 is Columbus
The 4 is Columbus
The 5 is Columbus
The 6 is Columbus
The 7 is Columbus
The 8 is Columbus
The 9 is Columbus
The 2 is (null)

为什么输出的数量是8呢,还有就是第二次查找键为2的值的时候为什么是null而在第一次却有值?

Csdn user default icon
上传中...
上传图片
插入图片