(第43-56行)我正在尝试为pset 5实现加载函数。我创建了一个嵌套的while循环,第一个用于迭代直到文件结束,另一个用于每个单词的结尾。我创建了char * c来存储我从字典中扫描的任何“字符串”,但是当我编译时
bool load(const char *dictionary)
{
//create a trie data type
typedef struct node
{
bool is_word;
struct node *children[27]; //this is a pointer too!
}node;
FILE *dptr = fopen(dictionary, "r");
if(dptr == NULL)
{
printf("Could not open dictionary\n");
unload();
return false;
}
//create a pointer to the root of the trie and never move this (use traversal *)
node *root = malloc(sizeof(node));
char *c = NULL;
//scan the file char by char until end and store it in c
while(fscanf(dptr,"%s",c) != EOF)
{
//in the beginning of every word, make a traversal pointer copy of root so we can always refer back to root
node *trav = root;
//repeat for every word
while ((*c) != '/0')
{
//convert char into array index
int alpha = ((*c) - 97);
//if array element is pointing to NULL, i.e. it hasn't been open yet,
if(trav -> children[alpha] == NULL)
{
//then create a new node and point it with the previous pointer.
node *next_node = malloc(sizeof(node));
trav -> children[alpha] = next_node;
//quit if malloc returns null
if(next_node == NULL)
{
printf("Could not open dictionary");
unload();
return false;
}
}
else if (trav -> children[alpha] != NULL)
{
//if an already existing path, just go to it
trav = trav -> children[alpha];
}
}
//a word is loaded.
trav -> is_word = true;
}
}
错误:
dictionary.c:52:23: error: multi-character character constant [-
Werror,-Wmultichar]
while ((*c) != '/0')
我认为这意味着'/0'
应该是一个单一的角色,但我不知道如何检查这个词的结尾!
我还收到另一条错误消息:
dictionary.c:84:1: error: control may reach end of non-void function [-Werror,-Wreturn-type]
}
我已经玩了一段时间了,这令人沮丧。请帮忙,如果你发现任何其他错误,我会很高兴的!
答案 0 :(得分:0)
你想要' \ 0' (空终止字符)而不是' / 0'。 另外,不要忘记在功能结束时返回一个bool!