連鎖次のコードは、私に次のエラーを与える分離しますか? コードは次のとおりです。要求「」ハッシュテーブルは
#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を得ることができますか? –
'table *** hashLinked' ??? 20年後に書いたコードでは、3重間接を使用する必要はありませんでした。再開する。 – trojanfoe