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

标签 c char load cs50

(第 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/

相关文章:

arrays - Kernighan 和 Ritchie 的主题 1.6

c++ - Char Star Array 参数未正确终止

java - 部分加载具有不同编码的大文本文件

Jquery div 到 div 加载仅适用于 Opera

c - 解析来自套接字的单个消息,其中包含 C 中的 2 个字符串

c - 使用多个文件 C 与将其全部放入单个文件相比,二进制文件有区别吗?

c - 在某个单词之后填充数组内的动态值

c - 是否有机器,其中 sizeof(char) != 1,或至少 CHAR_BIT > 8?

arrays - 函数并返回 const char*

css - 一些浏览器不加载 CSS 和组件布局[Joomla]