所以我得到这个错误。我知道该错误告诉我实际上并没有指向结构,但是我似乎无法弄清楚原因。这是我的代码。
typedef struct {
char * word;
char * defn;
} entry;
typedef struct {
int size;
struct entry **table;
} hashTable;
typedef hashTable * Dictionary;
Dictionary create(int initial_capacity, int delta_capacity){
Dictionary *new_table;
int i;
if ((new_table = malloc(sizeof(Dictionary))) == NULL){
return NULL;
}
if ((new_table->table = malloc(sizeof(entry *) * initial_capacity)) == NULL){
return NULL;
}
for(i=0; i < initial_capacity; i++){
new_table->table[i] = NULL;
}
return new_table;
}
这是我得到的两个编译器错误。
hashP.c: In function ‘create’:
hashP.c:15:16: error: request for member ‘table’ in something not a structure or union
if ((new_table->table = malloc(sizeof(entry *) * initial_capacity)) == NULL){
hashP.c:20:12: error: request for member ‘table’ in something not a structure or union
new_table->table[i] = NULL;
有人有想法么?
最佳答案
typedef hashTable * Dictionary;
通过将指针隐藏在typedef后面,您可以欺骗自己,使其无法理解自己的代码。因为
Dictionary *new_table
实际上是不是您想要的struct hashTable**
。根本不要将指针隐藏在typedef后面,所有问题都会消失。
关于c - 错误:在非结构或 union C中请求成员“表”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34038344/