for Pset5 speller my code for load along with the generation of hash codes leads to a segmentation fault. I realized it is because fscanf returns 0 after the last word of the dictionary, leading to -97 being returned but I cant figure out why it returns that instead of EOF,
unsigned int hash(const char *word)
{ int first = 0;
// TODO: Improve this hash function
// return 0-25 according to first letter of the word
int test = word[0]; //why does 2nd word[0] = 0
first = (int)word[0] - 97;
return first;
}
bool load(const char *dictionary)
{
FILE *dict = fopen(dictionary, "r");
char *tmpword;
//allocate space for the words
int code;
do
{
tmpword = malloc(LENGTH +8);
if (tmpword == NULL)
{
return 1;
}
node *n = malloc(sizeof(node));
fscanf(dict, "%s", tmpword); //after 2 words in dictionaries small, cant end fscanf returns 0 value
strcpy(n->word,tmpword);
code=hash(tmpword);
n->next = table[code];
table[code]=n;
}
while (*tmpword!= EOF); //has to be done earl
free(tmpword);
fclose(dict);
return false;
}