I've been working on Speller for a little while now, but I've run into a roadblock. I finished my first draft of speller and have been clearing up errors until this point. Here's my dictionary.c so far (I did not make any changes to dictionary.h).
// Implements a dictionary's functionality
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <strings.h>
#include "dictionary.h"
// Represents a node in a hash table
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
// Number of buckets in hash table
const unsigned int N = 65536;
// Words in loaded dictionary
int wordcount = 0;
// Hash table
node *table[N];
// Returns true if word is in dictionary else false
bool check(const char *word)
{
char textw[strlen(word) + 1];
for (int i = 0; i > strlen(word); i++)
{
textw[i] = word[i];
}
int hashnumber = hash(textw);
node *search = table[hashnumber];
while (search != NULL)
{
if (strcasecmp(search->word, textw) != 0)
{
search = search->next;
}
else
{
return true;
}
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
unsigned int hashed = 0;
for (int i = 0; i < strlen(word); i++)
{
hashed += (int) tolower(word[i]) - 'a';
}
return hashed % N;
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
return false;
}
char buffer[LENGTH + 1];
while (fscanf(file, "%s", buffer) != EOF)
{
node *new_node = malloc(sizeof(node));
if (new_node == NULL)
{
unload();
return false;
}
else
{
new_node->next = NULL;
strcpy(new_node->word, buffer);
int hashnumber = hash(new_node->word);
if (table[hashnumber] == NULL)
{
table[hashnumber] = buffer;
}
else
{
buffer->next = table[hashnumber];
table[hashnumber] = buffer;
}
}
wordcount++;
}
fclose(file);
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
return wordcount;
}
// Recursion function for unload, reference "yangrq1018" on GitHub
void clear(node *select)
{
if (select->next != NULL)
{
clear(select->next);
}
free(select);
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
for (int i = 0; i < N; i++)
{
if (table[i] != NULL)
{
clear(table[i]);
}
}
return true;
}
Currently, running make speller
produces the following errors
dictionary.c:88:35: error: incompatible pointer types assigning to 'node *' (aka 'struct node *')
from 'char [46]' [-Werror,-Wincompatible-pointer-types]
table[hashnumber] = buffer;
^ ~~~~~~
dictionary.c:92:23: error: member reference base type 'char' is not a structure or union
buffer->next = table[hashnumber];
~~~~~~^ ~~~~
dictionary.c:93:35: error: incompatible pointer types assigning to 'node *' (aka 'struct node *')
from 'char [46]' [-Werror,-Wincompatible-pointer-types]
table[hashnumber] = buffer;
^ ~~~~~~
3 errors generated.
Makefile:2: recipe for target 'speller' failed
make: *** [speller] Error 1
From the error message, I know that the load function is currently incorrect. I'm not entirely certain what to do about errors 1 and 3 (help50 was unable to explain any of these errors) though it explicitly states that the errors are due to "incompatible pointers" being assigned. Error 2 just went completely over my head!
I would greatly appreciate any help with this - these likely won't be the last issues with my code, but I want to at least compile it and start testing.