C Pset5使用trie实现加载

C Pset5使用trie实现加载,c,trie,cs50,C,Trie,Cs50,我在pset5中遇到了一些问题,实际上我不知道如何开始调试,我已经看了几遍课程了,但我没有取得任何进展 当我运行speller.c时,它给了我一个seg错误,我运行了调试器,它在For循环开始时崩溃,下面是我的代码: #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <ctype.h> #include <string.h> #include "d

我在pset5中遇到了一些问题,实际上我不知道如何开始调试,我已经看了几遍课程了,但我没有取得任何进展

当我运行speller.c时,它给了我一个seg错误,我运行了调试器,它在For循环开始时崩溃,下面是我的代码:

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

#include "dictionary.h"
// default dictionary
#define DICTIONARY "dictionaries/large"

//created the struct node
typedef struct node
{
    bool is_word;
    struct node * paths[27];
}
node;

int letter = 0;
char * word = NULL;

/**
 * Returns true if word is in dictionary else false.
 */ 
bool check(const char *word)
{
//todo
return false;
}

/**
 * Loads dictionary into memory. Returns true if successful else false.
 */
bool load(const char *dictionary)
{
//opens dictionary for reading
FILE *fp = fopen(DICTIONARY, "r");
if (fp == NULL)
{
    return false;
    unload();
}

//creates the root of the trie
node *root = malloc(sizeof(node));
root -> is_word = false;

node * trav = root;

char * word = NULL;

//start reading the file
while (fscanf(fp, "%s", word) != EOF)
{
    for (int i = 0; i < strlen(word); i++)
    {
        //assing wich path to take
        char c = fgetc(fp);
        if (isupper(c))
        {
            letter = tolower (c);
            letter = letter -'a';
        }
        else if (isalpha(c))
        {
            letter = c;
            letter = letter -'a';
        }
        else if (c == '\'')
        {
            letter = 26;
        }
        else if (c == '\0')
        {
            trav -> is_word = true;
        }
        if (trav -> paths[letter] == NULL)
        {
            node *new_node = malloc(sizeof(node));
            if (new_node == NULL)
            {
                return false;
               unload();
            }
            //point to new node
            trav -> paths[letter] = new_node;
        }
        else
        {
            trav = trav -> paths[letter];
        }
    }

}
if (fscanf(fp, "%s", word) == EOF)
{
    fclose(fp);
    return true;
}
return false;
}

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

/**
* Unloads dictionary from memory. Returns true if successful else false.
 */
bool unload(void)
{
// TODO
return false;
}
我也不知道如何将新节点指向下一个新节点,以及是否必须为它们指定不同的名称。例如,我要存储单词foo,所以我读了名为trav的节点,转到路径[5]f字母,检查它是否已经打开,如果不是空的话,我创建了一个名为new_node的节点,并将trav->path[5]指向它,然后我应该将trav更新为新节点,所以我将它指向它自己的路径[letter]

word是空指针。fscanf实际上不能为指向的指针分配内存。那么,当fscanf想要取消对word的引用以写入它读取的字符时会发生什么呢?不能取消对空指针的引用,它会导致未定义的行为。我建议您将word定义为数组


注意:答案取自与您的问题无关的评论

,但您知道退货会立即返回吗?返回后相同范围内的任何代码都不会执行,这就是所谓的死代码。在加载函数中,如果无法打开文件,则会出现死代码。至于您的问题,当您将其传递给fscanf时,word指向何处?fscanf没有为您分配内存。对不起,我已经更新了代码指向字,只是忘记在这里发布:char*word=NULL;是的,word是空指针。fscanf实际上不能为指向的指针分配内存。那么,当fscanf想要取消对word的引用以写入它读取的字符时会发生什么呢?不能取消对空指针的引用,它会导致错误。我建议您将word定义为数组。谢谢!成功了=