2017-09-05 1 views
0

(43 ~ 56 행) pset 5에 대한로드 기능을 구현하려고합니다. 중첩 된 while 루프를 만들었습니다. 처음에는 파일 끝까지 반복하고 각 단어의 끝까지 다른. 나는 사전에서 검색대로 "문자열"저장 숯불 *의 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'(종료 문자는 null)을 원합니다. 또한 함수가 끝날 때 bool을 반환하는 것을 잊지 마십시오!

+0

감사! 무슨 실수? – jasson

+0

@ jason Lim : 천만에! – cydef

관련 문제