[英]Incompatible pointer type - C
我有这种结构的结构,如以下代码所示:
typedef void* DataP;
typedef struct Table* TableP;
typedef const void* ConstKeyP;
typedef struct DataPointer
{
DataP dataP;
ConstKeyP key;
}DataPointer;
typedef struct HashTableSet
{
DataPointer *hashSet[MAX_ROW_ELEMENTS];
}HashTableSet;
typedef struct HashTableSet *HashTable;
typedef struct Table
{
int size, d;
HashTable hashTable;
}Table;
那就是结构。 现在这是创建哈希表的函数-
TableP createTable(size_t tableSize)
{
TableP tableP = (TableP)malloc(sizeof(Table));
HashTable table = (HashTable)malloc(sizeof(HashTable));
tableP->d = 1;
tableP->size = tableSize;
nullTable(tableP);
return tableP;
}
其中nullTable是我既要为实际集合提供内存又要给定null初始值的函数:
void nullTable(TableP tableP)
{
HashTable hashTable = tableP->hashTable;
int i = 0;
for(; i < tableP->size; i++)
{
int j = 0;
HashTableSet *hashTableSet = (HashTableSet*)malloc(sizeof(HashTableSet));
for (; j < MAX_ROW_ELEMENTS ; ++j)
{
DataPointer *data = (DataPointer*)malloc(sizeof(HashTableSet));
hashTableSet->hashSet[j] = data;
}
hashTable[i] = *hashTableSet;
//hashTable->hashSet = *hashTableSet;
hashTable += sizeof(HashTableSet);
}
}
这里出现第一个问题-在nullTable的最后几行中,我试图将hashTable的指针“跳转”到下一个设置的内存。 意味着我已经分配了我需要的所有空间,将我的hashTable [i]指向它,然后我向hashTable添加了sizeof(HashTableSet),因为我希望它能够到达另一个hashTableSet的下一个“位置”。 这已经充满了我做错了什么。
接下来,我遇到一个真正的问题,即了解此指针迭代在此函数中的工作方式:
DataP removeData(TableP table, const void* key)
{
TableP tableP = table;
int cell = table->hfun(key, table->size);
HashTable hashTable = table->hashTable;
HashTableSet *setPtr = hashTable->hashSet;
int i = 0;
int j = 0;
int d = 1;
while(d < tableP->d)
{
for (; i < MAX_ROW_ELEMENTS; i++)
{
if (table->fcomp(setPtr->hashSet[i]->key, key) == 0)
{
DataP dataP = setPtr->hashSet[i]->dataP;
setPtr->hashSet[i] = NULL;
table->freeKey((void*)key);
return dataP;
}
}
setPtr *= 2;
i = 0;
}
return NULL;
}
我希望setPtr指向表的第一个HashTableSet。 但我收到此错误消息:
warning: initialization from incompatible pointer type
HashTableSet *setPtr = hashTable->hashSet;
我在理解如何将setPtr带到下一个hashSet方面也遇到了问题,假设我设法根据自己的需要正确定义了setPtr。
根据您的定义:
typedef void* DataP;
typedef struct Table* TableP;
typedef const void* ConstKeyP;
typedef struct DataPointer
{
DataP dataP;
ConstKeyP key;
}DataPointer;
typedef struct HashTableSet
{
DataPointer *hashSet[MAX_ROW_ELEMENTS];
}HashTableSet;
typedef struct HashTableSet *HashTable;
typedef struct Table
{
int size, d;
HashTable hashTable;
}Table;
从您的代码:
HashTable hashTable = tableP->hashTable;
HashTableSet *setPtr = hashTable->hashSet;
所以HashTable
类型的hashTable
是struct HashTableSet*
。 到现在为止还挺好。 下一行是问题:HashTableSet * setPtr = hashTable-> hashSet;
hashTable->hashSet
不是HashTableSet类型!
这将起作用:
DataP removeData(TableP table, const void* key)
{
TableP tableP = table;
int cell = table->hfun(key, table->size);
HashTable hashTable = table->hashTable;
/*
* See change in the following line:
* Before the change you were trying to assign type
* `HashSet` into type `HashTableSet*`, this will
*/
HashTableSet *setPtr = hashTable;
int i = 0;
int j = 0;
int d = 1;
while(d < tableP->d)
{
for (; i < MAX_ROW_ELEMENTS; i++)
{
if (table->fcomp(setPtr->hashSet[i]->key, key) == 0)
{
DataP dataP = setPtr->hashSet[i]->dataP;
setPtr->hashSet[i] = NULL;
table->freeKey((void*)key);
return dataP;
}
}
setPtr *= 2;
i = 0;
}
return NULL;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.