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条回答

    报告相同问题?

    悬赏问题

    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!