不懂cs50拼写器编译错误
Don't understand cs50 speller compiler errors
我尝试寻找答案,但找不到足够具体的答案。在下面的代码中...
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;
}
像 for
循环或 rootptr -> is_word = false;
这样的东西是 语句 并且必须出现在函数中。
您不能在文件范围内放置语句。所以这整个区块都是非法的:
for(int i = 0; i < 27; i++)
{
rootptr->children[i] = NULL;
}
rootptr -> is_word = false;
您可以将它移动到您将在 main 开始时调用的函数:
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
:
Node root = {
.is_word = false,
.children[0] = NULL,
.children[1] = NULL,
// ...
.children[26] = NULL,
};
我尝试寻找答案,但找不到足够具体的答案。在下面的代码中...
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;
}
像 for
循环或 rootptr -> is_word = false;
这样的东西是 语句 并且必须出现在函数中。
您不能在文件范围内放置语句。所以这整个区块都是非法的:
for(int i = 0; i < 27; i++)
{
rootptr->children[i] = NULL;
}
rootptr -> is_word = false;
您可以将它移动到您将在 main 开始时调用的函数:
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
:
Node root = {
.is_word = false,
.children[0] = NULL,
.children[1] = NULL,
// ...
.children[26] = NULL,
};