我有一个数组,其中包含数组中每个索引的键和信息。阵列结构无法正常工作
此生成阵列
table_t *table_construct (int table_size, int probe_type)
{
int i;
table_t *hash;
if(table_size < 1) return NULL;
hash = malloc(sizeof(table_t));
hash->table = malloc(sizeof(list_t*) * table_size);
for(i=0; i < table_size - 1; i++)
{
hash->table[i] = NULL;
//hash->table[i]->next = NULL;
}
hash->size = table_size;
hash->probing_type = probe_type;
return hash;
}
所以我有list_t和table_t结构。我在我的下面这行代码是无法正常工作:
hash->table[item]->K = K;
它可以在我的这部分代码中可以看出:
int dec, item, hold;
item = hashing(hash,K);
hold = item;
if(hash->table[item] == NULL)
{
hash->table[item]->K = K;
hash->table[item]->I = I;
return 0;
}
当我GDB它,K是一个数字。
所以这里发生的是,我有我的表索引项目。然后我将K添加到索引的关键字中。当我的程序中的任何地方出现这条线时,我会得到一个seg故障。
你能看到我在这里做错了吗?
那么,你是否为你的'table'数组分配内存?然后你是否为'table'元素指向的每个'list_t'对象分配内存?你必须告诉我们你是如何做到的。 – AnT
不知道如何在'table'中分配项目,这将很难提供帮助... – John3136
现在我添加我的构造函数。 – user081608