I am trying to implement a recursive subfunction for speller. It works, but when I run check50, I'm told that there is a "Conditional jump or move depends on uninitialised value(s): (file: dictionary.c, line: 118. I'm not really sure what I'm supposed to initialize...is it my deleter node? My subfunction and unload function are at the bottom of the code. Thank you in advance!
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include "dictionary.h"
#include <ctype.h>
// Represents a node in a hash table
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
//new function
void recurse(node *deleter);
// Number of buckets in hash table
const unsigned int N = 26;
int dic_size =0;
// Hash table
node *table[N];
// Returns true if word is in dictionary else false
bool check(const char *word)
{
int key = hash(word);//use hash to find out where the word is stored
for (node *tmp = table[key]; tmp != NULL; tmp = tmp->next)//go to the table value and loop until word is found or until null
{
int find = strcasecmp(tmp->word, word); //compares the pointer and the word
if (find == 0) //if you find the word, return true and exit program
return true;
}
return false;//otherwise, return false
}
// Hashes word to a number
unsigned int hash(const char *word)
{
if (isupper(word[0]) != 0)
return word[0]-65;
else
return word[0]-97;
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
for (int i = 0; i < N; i++)//malloc space for nodes to initial table bucket nodes
{
table[i] = malloc(sizeof(node));
if (table[i] == NULL)
{
return false;
}
}
FILE *diction_pointer = fopen(dictionary, "r");// open the dictionary file, set pointer dictionary to it
if (diction_pointer == NULL)//check if dictionary was able to open
return false;
char dic_word[46]; //declare character array that can hold word values
int counter = -1; //arbitrrary number that is not the first value lol
while (fscanf(diction_pointer, "%s", dic_word) != EOF)// while eof is not met
{
node *dic = malloc (sizeof(node));//create a node for each word by mallocing memory
dic->next = NULL;
if (dic == NULL) //make sure malloc-ed memory isn't nul
return false;
strcpy (dic->word, dic_word);//copy dic_word into node dic's word
int bucket = hash(dic_word); //hash function returns bucket int
if (bucket != counter)//this is the first+ iteration
{
*table[bucket] = *dic; //assigns the value in dic into the space in table bucket
table[bucket]->next = NULL; //then set the pointer at node dic to array table[bucket]
free(dic);
}
else
{
dic->next = table[bucket];
table[bucket] = dic; //otherwise, just set teh table pointer to dic
}
counter = bucket;
dic_size++;
}
fclose(diction_pointer);
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
return dic_size;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
for (int i = 0; i < N; i++)// for all of the tables in N
{
recurse (table[i]); // call recursive funcion
}
return true;
}
void recurse(node *deleter)
{
if (deleter->next != NULL)//if temp pointer is null
recurse(deleter->next);
free (deleter);//free the current node
}