我试过寻找答案但找不到足够具体的东西。在以下代码中......
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;
}
答案 0 :(得分:1)
您不能在文件范围内放置语句。所以整个块都是非法的:
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,
};
答案 1 :(得分:0)
for
循环或rootptr -> is_word = false;
之类的内容是语句,必须出现在函数内。