多字符字符常量 [-Werror,-Wmultichar]
multi-character character constant [-Werror,-Wmultichar]
(第 43-56 行)我正在尝试为 pset 5 实现加载函数。我创建了一个嵌套的 while 循环,第一个循环迭代到文件末尾,另一个循环迭代到每个单词的末尾。我创建了 char *c 来存储我从字典中扫描的任何内容 "string",但是当我编译时
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”。
此外,不要忘记在函数末尾添加 return 布尔值!
(第 43-56 行)我正在尝试为 pset 5 实现加载函数。我创建了一个嵌套的 while 循环,第一个循环迭代到文件末尾,另一个循环迭代到每个单词的末尾。我创建了 char *c 来存储我从字典中扫描的任何内容 "string",但是当我编译时
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”。 此外,不要忘记在函数末尾添加 return 布尔值!