得到“节点没有成员” GCC (c99) 中基本 trie 结构的错误
我正在尝试用普通的旧 C 语言实现基本的 trie 结构,但在编译时出现“节点没有成员”错误。
这是我的结构定义:
typedef struct node {
bool word;
struct node *alpharray[26];
} node;
我尝试初始化一些节点如下:
node *root = malloc(sizeof(node));
node *nptr = malloc(sizeof(node));
nptr = root;
上面的代码使用“迭代器”作为指针,它将在创建时循环遍历给定的 trie 结构。这是它的工作原理:
while (ch != '\n' && !feof(fp))
{
//get the array index value for the current character (ch)
//getCharNum() returns an int 0-25 corresponding to a slot for a node's alpharray
int char_num = getCharNum(ch);
//if a character has not been put in the given slot yet, make a new node and point
if (nptr->alpharray[char_num] == NULL)
{
node *newnode = malloc(sizeof(node));
nptr->alpharray[char_num] = newnode;
nptr = newnode;
}
//otherwise, move the main pointer to the next node in the chain of nodes
else
{
nptr = nptr->alpharray[char_num];
}
//get the next character
fread(&ch, sizeof(char), 1, fp);
}
每次我尝试访问和/或更改任何给定的节点属性时,编译时都会重复出现错误。我还尝试了 *node.alphaarray 表示法,据我了解,它与上面的表示法等效,因为节点将被取消引用。
我知道这可能是我忽略的一些基本问题,但我似乎无法在任何地方找到解决方案。有想法吗?
这是编译器输出:
gcc -ggdb -std=c99 -Wall -Werror -c -o dictionary.o dictionary.c
dictionary.c:30:15: error: expected ':', ',', ';', '}' or '__attribute__' before '=' token
dictionary.c: In function 'load':
dictionary.c:74:21: error: 'node' has no member named 'alpharray'
dictionary.c:77:21: error: 'node' has no member named 'alpharray'
dictionary.c:84:28: error: 'node' has no member named 'alpharray'
dictionary.c:71:17: error: variable 'char_num' set but not used [-Werror=unused-but-set-variable]
dictionary.c:93:16: error: 'node' has no member named 'word'
cc1: all warnings being treated as errors
make: *** [dictionary.o] Error 1
I'm trying to implement a basic trie structure in plain old C, and I'm getting a "node has no member" error upon compilation.
Here is my structure definition:
typedef struct node {
bool word;
struct node *alpharray[26];
} node;
My attempts to initialize some nodes are as follows:
node *root = malloc(sizeof(node));
node *nptr = malloc(sizeof(node));
nptr = root;
The above code uses "iterator" as the pointer that will cycle through the given trie structure as it's being created. Here's how that works:
while (ch != '\n' && !feof(fp))
{
//get the array index value for the current character (ch)
//getCharNum() returns an int 0-25 corresponding to a slot for a node's alpharray
int char_num = getCharNum(ch);
//if a character has not been put in the given slot yet, make a new node and point
if (nptr->alpharray[char_num] == NULL)
{
node *newnode = malloc(sizeof(node));
nptr->alpharray[char_num] = newnode;
nptr = newnode;
}
//otherwise, move the main pointer to the next node in the chain of nodes
else
{
nptr = nptr->alpharray[char_num];
}
//get the next character
fread(&ch, sizeof(char), 1, fp);
}
The error I get when compiling repeats each time I try to access and/or change any given node attribute. I have also tried the *node.alpharray notation, which as I understand is the equivalent to the above because node will be dereferenced.
I know it's probably something basic I'm overlooking, but I can't seem to find a solution anywhere. Ideas?
Here is the compiler output:
gcc -ggdb -std=c99 -Wall -Werror -c -o dictionary.o dictionary.c
dictionary.c:30:15: error: expected ':', ',', ';', '}' or '__attribute__' before '=' token
dictionary.c: In function 'load':
dictionary.c:74:21: error: 'node' has no member named 'alpharray'
dictionary.c:77:21: error: 'node' has no member named 'alpharray'
dictionary.c:84:28: error: 'node' has no member named 'alpharray'
dictionary.c:71:17: error: variable 'char_num' set but not used [-Werror=unused-but-set-variable]
dictionary.c:93:16: error: 'node' has no member named 'word'
cc1: all warnings being treated as errors
make: *** [dictionary.o] Error 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您忘记初始化数组中的指针。尝试:
You are forgetting to initialise the pointers in the array. Try:
如果将节点初始化为:
您为节点本身创建空间,但不是为每个指针创建空间,
因为 alphaarray 将为 NULL,因此您不能使用它直接获取它的元素。您也必须为这些元素进行 malloc。
If you initialize your node as :
You create space for the node itself, but not for each of the pointers in
Since alpharray will be NULL you cannot use it to get an element of it directly. You have to malloc for those elements too.
你的“alphaarray”应该声明为:
因为在行中:
你正在将指针分配给一个结构(这是非法的)。
您的节点声明应该是:
当编译器看到您的“alphaarray”的(原始)声明时,它无法弄清楚
数组中每个元素的大小(因为类型尚未完全定义)。
这就是为什么除了指针数组之外它不能以任何其他方式工作(您所做的是“结构节点”数组,而不是指向“结构节点”的指针数组 - 您应该做什么)。
Your 'alpharray' should be declared as:
because in the line:
you ares assigning pointer to a structure (which is illegal).
Your node declaration should be:
When compiler sees your (original) declaration of 'alpharray' it cannot figure out
the size of each element in an array (because the type is not yet fully defined).
That is why it cannot work any other way than with a array of pointers (what you did is an array of 'struct node', not an array of pointers to 'struct node' - what you should do).