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

    报告相同问题?

    悬赏问题

    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 有没有帮写代码做实验仿真的
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥30 vmware exsi重置后登不上
    • ¥15 c++头文件不能识别CDialog
    • ¥15 Excel发现不可读取的内容
    • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
    • ¥20 yolov5自定义Prune报错,如何解决?
    • ¥15 电磁场的matlab仿真