2017-04-15 11 views
-1

私は答えを探してみましたが、十分に具体的なものは見つかりませんでした。次のコードで...cs50 spellerコンパイラのエラーを理解していません

typedef struct node 
{ 
    bool is_word; 
    struct node *children[27]; 
}Node; 

Node root; 
Node *rootptr = &root; 

for(int i = 0; i < 27; i++) 
{ 
     rootptr->children[i] = NULL; 
} 
rootptr -> is_word = false; 

私はかなり理解していない、次のコンパイラエラーが発生しています。ここで

~/workspace/pset5/speller/ $ make 
clang -ggdb3 -O0 -Qunused-arguments -std=c11 -Wall -Werror -c -o 
speller.o speller.c 
clang -ggdb3 -O0 -Qunused-arguments -std=c11 -Wall -Werror -c -o 
dictionary.o dictionary.c 
dictionary.c:22:5: error: expected identifier or '(' 
    for(int i = 0; i < 27; i++) 
    ^
dictionary.c:26:5: error: unknown type name 'rootptr' 
    rootptr -> is_word = false; 
    ^
dictionary.c:26:13: error: expected identifier or '(' 
    rootptr -> is_word = false; 
     ^
3 errors generated. 
make: *** [dictionary.o] Error 1  

はspeller.cのための私の完全なコード(私はそれが他の論理エラーを持っている知っているが、私はちょうどこの最初のまわりで私の頭を取得しようとしているです。

/** 
* Implements a dictionary's functionality. 
*/ 

#include <stdbool.h> 
#include "dictionary.h" 
#include <string.h> 
#include <stdlib.h> 
#include <stdio.h> 



typedef struct node 
{ 
    bool is_word; 
    struct node *children[27]; 
}Node; 

Node root; 
Node *rootptr = &root; 

for(int i = 0; i < 27; i++) 
{ 
     rootptr->children[i] = NULL; 
} 
rootptr -> is_word = false; 

unsigned int counter = 0; 

/** 
* Returns true if word is in dictionary else false. 
*/ 
bool check(const char *word) 
{ 
    Node *travptr = rootptr; 
    int letter = 0; 
    for(int i = 0, n = strlen(word); i < n; i++) 
    { 
     letter = word[i]; 
     letter = letter - 96; 

     if(travptr -> children[letter] != NULL) 
     { 
      if(i < n-1) 
      { 
       travptr = travptr -> children[letter]; 
      } 
      else return travptr -> is_word; 
     } 
     else return false; 
    } 
    return false; 
} 

/** 
* Loads dictionary into memory. Returns true if successful else false. 
*/ 
bool load(const char *dictionary) 
{ 
    char *word = malloc(LENGTH +1); 
    int letter = 0; 
    Node *travptr = NULL; 

    FILE *dict = fopen(dictionary, "r"); 
    if(dict == NULL) 
    { 
     return false; 
    } 

    while(fscanf(dict, "%s", word) != EOF) 
    { 
     travptr = rootptr; 

     for(int i = 0, n = strlen(word); i < n; i++) 
     { 
      letter = word[i]; 
      letter = letter - 96; 

      if(travptr -> children[letter] == NULL) 
      { 
       Node *newptr = malloc(sizeof(Node)); 
       if(newptr == NULL) 
       { 
        unload(); 
        return false; 
       } 
       for(int i = 0; i < 27; i++) 
       { 
        newptr -> children[i] = NULL; 
       } 
       newptr -> is_word = false; 
       if(i == n-1) 
        newptr -> is_word = true; 

       travptr = newptr ; 

      } 
      else travptr = travptr -> children[letter]; 
     } 
     counter++; 
    } 
    fclose(dict); 
    return true; 

} 

/** 
* Returns number of words in dictionary if loaded else 0 if not yet loaded. 
*/ 
unsigned int size(void) 
{ 
    // TODO 
    return counter; 
} 

/** 
* Unloads dictionary from memory. Returns true if successful else false. 
*/ 
bool unload(void) 
{ 
    Node *travptr = rootptr; 
    for(int i = 0; i < 27; i++) 
    { 
     if(travptr -> children[i] != NULL) 
     { 
      travptr = travptr -> children[i]; 
      unload(); 
     } 
     else free(travptr); 
    } 
    return true; 

}

+1

すべての実行可能コードステートメントは、関数内にある必要があります。 –

答えて

1

ステートムを配置することはできませんファイルスコープのents。したがって、このブロック全体が違法である:

void init_root(void) 
{ 
    for(int i = 0; i < 27; i++) 
    { 
      rootptr->children[i] = NULL; 
    } 
    rootptr -> is_word = false; 
} 

int main(void) 
{ 
    init_root(); 
} 

それとももう少し退屈なルートを行くとしrootを初期化します。

for(int i = 0; i < 27; i++) 
{ 
     rootptr->children[i] = NULL; 
} 
rootptr -> is_word = false; 

あなたがメインの開始時に呼び出す関数に移動することができます適切な初期設定子:

Node root = { 
    .is_word = false, 
    .children[0] = NULL, 
    .children[1] = NULL, 
    // ... 
    .children[26] = NULL, 
}; 
0

forループまたはrootptr -> is_word = false;のようなものはステートメントであり、関数内に表示する必要があります。

関連する問題