load() 函数的问题(CS50 问题集 5:拼写)

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

我正在研究 CS50 的问题集 5(拼写),但我不明白 load() 函数哪里出了问题。当我尝试编译时,第 36 行和第 79 行出现错误。但我不明白如何解决它。我是 CS 的新手,所以如果这很明显,我很抱歉。 我认为我的问题与 strcpy 和 strcmp 函数有关,但据我所知,我正在按照 CS50 手册页中的说明使用这些函数。但是,很明显我遗漏了一些东西,否则它会起作用。 提前致谢!!

// 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;
}

节点定义与发行版代码不一样; word 应该是一个字符数组而不是一个字符指针数组。 node->word 是字符串函数中使用的错误类型。