2017-09-05 4 views
0

(lignes 43-56) J'essaie d'implémenter la fonction load pour pset 5. J'ai créé une boucle while imbriquée, la première pour l'itération jusqu'à la fin du fichier et la autre jusqu'à la fin de chaque mot. J'ai créé char * c pour stocker tout ce que « string » je scanne du dictionnaire, mais quand je compileConstante de caractères multi-caractères [-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; 

    } 
} 

Erreur:

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

Je pense que cela signifie '/0' devrait être un seul caractère, mais je ne Je ne sais pas comment je pourrais vérifier la fin du mot! Je reçois aussi un autre message d'erreur indiquant:

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

Je joue avec elle pendant un certain temps maintenant, et il est frustrant. S'il vous plaît aider, et si vous trouvez des bugs supplémentaires, je serai heureux!

+3

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

+1

@rsp Ou juste 0, pas de guillemets, pas de confusion. – cnicutar

+0

Ou '' ??/0'' (dans le cas où vous n'avez pas '\'). –

Répondre

0

Vous voulez '\ 0' (caractère de fin nul) au lieu de '/ 0'. En outre, n'oubliez pas de retourner un booléen à la fin de votre fonction!

+0

Merci! Quelle erreur – jasson

+0

@jason Lim: De rien! – cydef