As the title suggests I having a bit of trouble with my if
and else
down at the bottom of my code. Errors are arising that say incompatible pointer to integer conversion. I am a bit confused as to whether to the problem lies with the code above the issues, or the actual code in the if
and else
statements. I am still a bit fuzzy on some of the dereferencing concepts and such.
/**
* Loads dictionary into memory. Returns true if successful else false.
*/
// hash function
int hash_fxn (char* key)
{
// hash on 1st letter
int hash = toupper(key[0]) - 'A';
return hash % 26;
}
bool load(const char* dictionary)
{
// TODO
// word counter
int wordcount = 0;
// open dictionary
FILE* dict = fopen(dictionary, "r");
if (dict == NULL)
{
printf("Dictionary could not be loaded\n");
return false;
}
// create the base array of the alphabet plus a space for apos.
int alphabet[26];
for (int i = 0; i < 27; i++)
alphabet[i] = 0;
// create a node for a separate chaining hash table
typedef struct node
{
// array to store word
char word[LENGTH + 1];
//create a pointer to the next node
struct node* next;
}
node;
// while the dictionary is not finished
while (!feof(dict))
{
// create space for new_node
node* new_node = malloc(sizeof(node));
// read word from dictionary and place it in new node
fscanf(dict, "%s", new_node->word);
// add to wordcount
wordcount++;
// make the new_node not point to anything
new_node->next = NULL;
// generate hash value
int hash_value = hash_fxn(new_node->word);
// if there is nothing in the hash bin
if (alphabet[hash_value] == 0)
alphabet[hash_value] = new_node;
// if something is in bin insert at the head of the bin
else
{
new_node->next = &alphabet[hash_value];
alphabet[hash_value] = new_node;
}
}
return true;
}