error: request for member ‘table’ in something not a structure or union C
error: request for member ‘table’ in something not a structure or union C
所以我收到了这个错误。我知道错误告诉我我实际上并没有指向结构,但我似乎无法弄清楚原因。这是我的代码。
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 后面,所有问题都会消失。
所以我收到了这个错误。我知道错误告诉我我实际上并没有指向结构,但我似乎无法弄清楚原因。这是我的代码。
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 后面,所有问题都会消失。