下面的代码给我下面的错误:请求成员“”不是一个结构或联合错误hashTable中分离链
error: request for member ‘name’ in something not a structure or union
error: request for member ‘name’ in something not a structure or union
error: request for member ‘data’ in something not a structure or union
error: request for member ‘next’ in something not a structure or union
我该如何解决呢? 代码是:
#define SIZE 5
typedef 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;
}
如果您甚至无法编译您的代码,如何获得seg-fault? – 2012-03-03 19:20:33
'表*** hashLinked'?我从来不需要在20年内编写的任何代码中使用三重间接。重新开始。 – trojanfoe 2012-03-03 19:23:00