iple 2014-07-25 03:03
浏览 2250

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而在第一次却有值?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 sub地址DHCP问题
    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办
    • ¥15 kylin启动报错log4j类冲突
    • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大