以下代码给了我以下错误:error: request for member ‘name’ in something not a structure or unionerror: request for member ‘name’ in something not a structure or unionerror: request for member ‘data’ in something not a structure or unionerror: request for member ‘next’ in something not a structure or union我该如何解决?代码是:#define SIZE 5typedef struct hashTable{ int data; char *name; struct hashTable *next;} table;int hash_function(int value){ return value % SIZE;}int insert(char *inFileName, table ***hashLinked){ FILE *inFile; int val = -1; char str[30]; int probe; if ((inFile = fopen(inFileName, "r")) == NULL) { fprintf(stderr,"Error opening input file, %s\n", inFileName); return -1; } while(fscanf(inFile,"%s %d",str,&val) == 2) { probe = hash_function(val); if(hashLinked[probe] == NULL) { **hashLinked[probe] = malloc(sizeof(table)); **hashLinked[probe]->name = (char *)malloc((strlen(str) + 1) * sizeof(char*)); strcpy(**hashLinked[probe]->name,str); **hashLinked[probe]->data = val; **hashLinked[probe]->next = NULL; } else { table* hashLinkedNode = *hashLinked[probe]; while(hashLinkedNode->next!=NULL) { hashLinkedNode = hashLinkedNode->next; } hashLinkedNode->next = malloc(sizeof(table)); hashLinkedNode->next->name = (char *)malloc((strlen(str) + 1) * sizeof(char*)); strcpy(hashLinkedNode->next->name,str); hashLinkedNode->next->data = val; hashLinkedNode->next->next = NULL; } } fclose(inFile); return 0;}void printList(BookNode *hd){ for ( ; hd != NULL; hd = hd->next) { printf("[%s,%d]", hd->name, hd->isbn); if (hd->next) printf(" -> "); } printf("\n");}void printHashTable(BookNode **temp){ BookNode *tmp = NULL; int i; for(i=0;i<SIZE;i++) { tmp = temp[i]; while(tmp) { printf("%s %d",tmp->name, tmp->isbn); tmp=tmp->next; } }}int main(int argc, char *argv[]){ table **hashLinked[SIZE]; insert(argv[1],&hashLinked); printHashTable(**hashLinked); return 0;} 最佳答案 一个问题是调用insert时使用的不是您声明的类型,table **hashLinked[SIZE];insert(argv[1],&hashLinked);hashLinked是一个指向table的指针的指针数组,因此&hashLinked是一个指向table的指针的指针数组的指针,但是insert被声明为采用指向。我对自己真正想出要做什么的信心不足,但是似乎合理的是,您需要更少的间接级别。我相信传递table的原因是您希望在&hashLinked中修改hashLinked,但这已经通过传递insert本身完成了,您不必传递其地址。这将使传递的类型与声明的类型兼容,因为hashLinked作为函数参数成为其第一个元素hashLinked的指针。然后,您在table ***中使用不一致的间接计数,并且将insert和*的优先级设置为错误,这会导致“请求成员而不是结构或联合”错误。 ->被解析为**hashLinked[probe]->name,因此尝试访问**(hashLinked[probe]->name)的name成员,然后将其取消引用两次。使用参数类型table *时,正确的访问权限应为table ***,每个(*hashLinked[probe])->name获取一个table **,取消引用一次即可获取hashLinked[probe]并访问其(指针)成员table *。但是,您检查name,如果是,**hashLinked[probe] = malloc(sizeof(table));这是保证的空指针取消引用。通过检查和下面的代码,我相信您实际上希望具有if (hashLinked[probe] == NULL)的参数类型,其中table **参数是hashLinked的链接列表的数组,这使代码更容易理解。填写table类型并修改一些变量和参数,我得出#include <stdlib.h>#include <stdio.h>#include <string.h>#define SIZE 5typedef struct hashTable { int data; char *name; struct hashTable *next;} table;typedef struct book { int isbn; char *name; struct book *next;} BookNode;int hash_function(int value){ return value % SIZE;}int insert(char *inFileName, table **hashLinked){ FILE *inFile; int val = -1; char str[30]; int probe; if ((inFile = fopen(inFileName, "r")) == NULL) { fprintf(stderr,"Error opening input file, %s\n", inFileName); return -1; } while(fscanf(inFile,"%s %d",str,&val) == 2) { probe = hash_function(val); if(hashLinked[probe] == NULL) { hashLinked[probe] = malloc(sizeof(table)); hashLinked[probe]->name = (char *)malloc((strlen(str) + 1) * sizeof(char*)); strcpy(hashLinked[probe]->name,str); hashLinked[probe]->data = val; hashLinked[probe]->next = NULL; } else { table* hashLinkedNode = hashLinked[probe]; while(hashLinkedNode->next!=NULL) { hashLinkedNode = hashLinkedNode->next; } hashLinkedNode->next = malloc(sizeof(table)); hashLinkedNode->next->name = (char *)malloc((strlen(str) + 1) * sizeof(char*)); strcpy(hashLinkedNode->next->name,str); hashLinkedNode->next->data = val; hashLinkedNode->next->next = NULL; } } fclose(inFile); return 0;}void printList(BookNode *hd){ for ( ; hd != NULL; hd = hd->next) { printf("[%s,%d]", hd->name, hd->isbn); if (hd->next) printf(" -> "); } printf("\n");}void printHashTable(table **temp){ table *tmp = NULL; int i; for(i = 0; i < SIZE; i++) { tmp = temp[i]; while(tmp) { printf("%s %d",tmp->name, tmp->data); tmp = tmp->next; } }}int main(int argc, char *argv[]){ if (argc < 2) return -1; table *hashLinked[SIZE]; insert(argv[1],hashLinked); printHashTable(hashLinked); return 0;}它可以无警告地编译,并且看起来可以按照您的预期进行。
10-04 22:54