2017-09-05 65 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