2017-09-05 270 views
0

(第43-56行)我试图为pset 5实现加载函数。我创建了一个嵌套的while循环,第一个用于迭代,直到文件的结尾和其他直到每个单词结束。我创建的char * C存放任何“字符串”我是从字典扫描,但是当我编译多字符字符常量[-Werror,-Wmultichar]

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] 
    } 

我一直在玩了一段时间,现在,它是令人沮丧的。请帮助,如果您发现任何其他错误,我会很高兴!

+3

''/ 0'' ---->''\ 0'' – rsp

+1

@rsp或者只是0,没有引号,没有混淆。 – cnicutar

+0

或''??/0''(如果你没有'\')。 –

回答

0

你想要'\ 0'(空终止字符)而不是'/ 0'。 此外,不要忘记返回一个布尔函数的结尾!

+0

谢谢!什么错误 – jasson

+0

@jason Lim:不客气! – cydef