gpt4 book ai didi

c - 多字符字符常量 [-Werror,-Wmultichar]

转载 作者:行者123 更新时间:2023-11-30 16:42:02 28 4
gpt4 key购买 nike

(第 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”。另外,不要忘记在函数末尾返回一个 bool !

关于c - 多字符字符常量 [-Werror,-Wmultichar],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46050598/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com