C 会员申请'';在非结构或联合错误哈希表中,使用单独的链接
以下代码给出了以下错误:C 会员申请'';在非结构或联合错误哈希表中,使用单独的链接,c,struct,hashtable,C,Struct,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 membe
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;
}
#定义尺寸5
typedef结构哈希表{
int数据;
字符*名称;
结构哈希表*下一步;
}表;
int散列函数(int值)
{
返回值%SIZE;
}
int insert(字符*填充名,表***哈希链接)
{
文件*填充;
int val=-1;
char-str[30];
int探针;
if((inFile=fopen(inFileName,“r”))==NULL)
{
fprintf(stderr,“打开输入文件%s\n时出错”,inFileName);
返回-1;
}
而(fscanf(填充、%s%d)、str和val)==2)
{
探测=散列函数(val);
if(hashLinked[probe]==NULL)
{
**hashLinked[probe]=malloc(sizeof(table));
**hashLinked[probe]->name=(char*)malloc((strlen(str)+1)*sizeof(char*);
strcpy(**hashLinked[probe]->名称,str);
**hashLinked[probe]->data=val;
**hashLinked[probe]->next=NULL;
}
其他的
{
表*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(infle);
返回0;
}
无效打印列表(BookNode*hd)
{
对于(;hd!=NULL;hd=hd->next)
{
printf(“[%s,%d]”,hd->name,hd->isbn);
如果(高清->下一步)
printf(“->”);
}
printf(“\n”);
}
无效打印哈希表(BookNode**temp)
{
BookNode*tmp=NULL;
int i;
对于(i=0;在南,tmp->isbn);
tmp=tmp->next;
}
}
}
int main(int argc,char*argv[])
{
表**哈希链接[大小];
插入(argv[1],&hashLinked);
printHashTable(**哈希链接);
返回0;
}
一个问题是调用insert
时使用的不是您声明的类型
table **hashLinked[SIZE];
insert(argv[1],&hashLinked);
hashLinked
是指向表
的指针数组,因此hashLinked
是指向表
指针数组的指针,但insert
声明为指向表
指针的指针。我不太相信我真的明白了你的意图,但似乎合理的是,你想要的间接层次更少。我相信传递&hashLinked
的原因是您希望在insert
中修改hashLinked
,但这已经通过传递hashLinked
本身完成了,您无需传递其地址。这将使传递的类型与声明的类型兼容,因为作为函数参数,hashLinked
成为指向其第一个元素的指针,表***
然后在insert
中使用不一致的间接计数,并将*
和->
的优先级设置错误,这会导致“请求非结构或联合中的成员”错误<代码>**hashLinked[probe]->name被解析为**(hashLinked[probe]->name)
,因此尝试访问表*
的name
成员,然后将其取消引用两次。使用参数类型table***
,正确的访问权限是(*hashLinked[probe])->name
,根据hashLinked[probe]
获取表**
,取消对该表的引用以获取表*
,并访问其(指针)成员name
。但是,如果(hashLinked[probe]==NULL),则检查,如果是
**hashLinked[probe] = malloc(sizeof(table));
这是一个保证的空指针解引用。通过检查和下面的代码,我相信您实际上想要一个参数类型table**
,hashLinked
参数是table
的链接列表数组,这使得代码更容易理解。填写BookNode
类型并调整一些变量和参数,我得到
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define SIZE 5
typedef 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;
}
#包括
#包括
#包括
#定义尺寸5
typedef结构哈希表{
int数据;
字符*名称;
结构哈希表*下一步;
}表;
typedef结构书{
国际标准书号;
字符*名称;
结构书*下一页;
}图书节点;
int散列函数(int值)
{
返回值%SIZE;
}
int insert(字符*填充名,表**哈希链接)
{
文件*填充;
int val=-1;
char-str[30];
int探针;
if((inFile=fopen(inFileName,“r”))==NULL)
{
fprintf(stderr,“打开输入文件%s\n时出错”,inFileName);
返回-1;
}
而(fscanf(填充、%s%d)、str和val)==2)
{
探测=散列函数(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;
}
其他的
{
表*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(infle);
返回0;
}
无效打印列表(B)