2017-09-05 1 views
0

(Zeilen 43-56) Ich versuche, Ladefunktion für Pset 5 zu implementieren. Ich erstellte eine geschachtelte While-Schleife, zunächst für das Iterieren bis zum Ende der Datei und der andere bis Ende jedes Wortes. Ich habe char * c zu speichern, was auch immer „string“ Ich komme aus Wörterbuch scannen, aber wenn ich kompilierenMulti-Zeichen-Zeichen Konstante [-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; 

    } 
} 

Fehler:

dictionary.c:52:23: error: multi-character character constant [- 
     Werror,-Wmultichar] 
     while ((*c) != '/0') 

Ich denke, das bedeutet '/0' sollte ein einzelnes Zeichen sein, aber ich don Ich weiß nicht, wie ich sonst nach dem Ende des Wortes suchen würde! Ich habe auch eine andere Fehlermeldung, die besagt:

dictionary.c:84:1: error: control may reach end of non-void function [-Werror,-Wreturn-type] 
    } 

Ich habe jetzt schon eine Weile mit ihm gespielt, und es ist frustrierend. Bitte helfen Sie, und wenn Sie irgendwelche zusätzlichen Fehler finden, werde ich mich freuen!

+3

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

+1

@rsp Oder nur 0, keine Anführungszeichen, keine Verwirrung. – cnicutar

+0

Oder '' ??/0'' (falls Sie kein '\' haben). –

Antwort

0

Sie möchten '\ 0' (Nullzeichen) anstelle von '/ 0'. Vergessen Sie außerdem nicht, am Ende Ihrer Funktion einen Bool zurückzugeben!

+0

Danke! Was für ein Fehler – jasson

+0

@jason Lim: Gern geschehen! – cydef

Verwandte Themen