Search code examples
ccompiler-errorscs50strcmpstrcpy

Problem with load() function (CS50 Problem Set 5: Speller)


I'm working on CS50's Problem Set 5 (Speller) and I don't understand where I am going wrong with the load() function. I am getting errors in lines 36 and 79 when I try to compile. But I don't understand how to fix it. I am new to CS so I am sorry if this is something obvious. I think my issue has something to do with the strcpy and strcmp functions, but as far as I understand I am using the functions as instructed in the CS50 manual pages. However, clearly I am missing something otherwise it would be working. Thank you in advance!!

// Implements a dictionary's functionality

#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "dictionary.h"

// Represents a node in a hash table
typedef struct node
{
    char *word[LENGTH + 1];
    struct node *next;
}
node;

// TODO: Choose number of buckets in hash table
unsigned long word_count;
const unsigned int N = 26;

// Hash table
node *table[N];

// Returns true if word is in dictionary, else false
bool check(const char *word)
{
    // Find hash location for current word
    int h = hash(word);
    // Create cursor to keep track of location on linked list at hash location
    node *cursor = table[h] -> next;
    while (cursor != NULL)
    {
        // If word is found
        if (strcmp(cursor -> word, word) == 0)
        {
            return true;
        }
        // If word is not at current cursor location
        else
        {
            cursor = cursor -> next;
        }
    }
    // If word is not found in linked list at hash location
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    // TODO: Improve this hash function
   return toupper(word[0]) - 'A';
}

// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
    // Initialise number of words loaded from dictionary
    word_count = 0;
    // Open dictionary file from command line
    FILE *dict = fopen(dictionary, "r");
    if (dict == NULL)
    {
        return false;
    }
    // Read words from dictionary one by one
    char buffer[LENGTH + 1];
    while (fscanf(dict, "%s", buffer) != EOF)
    {
        // Create new node for current word
        node *n = malloc(sizeof(node));
        if (n == NULL)
        {
            return false;
        }
        // Copy current word to created node
        strcpy(n -> word, buffer);

        // Assign node to the hash table
        int h = hash(buffer);
        // Node does not already exist at hash location
        if (table[h] -> next == NULL)
        {
            n -> next = NULL;
            table[h] -> next = n;
        }
        // Node already exists at hash location
        else
        {
            n -> next = table[h] -> next;
            table[h] -> next = n;
        }
        // Update counter for words loaded from dictionary
        word_count++;
    }
    fclose(dict);
    // Dictionary successfully loaded
    return true;
}

// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
    return word_count;
}

// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
    // TODO
    return false;
}

Solution

  • The node definition is not the same as the distro code; word should be a char array not an array of char pointers. node->word is the wrong type for use in the string functions.