得到“节点没有成员” GCC (c99) 中基本 trie 结构的错误

发布于 2025-01-08 06:17:20 字数 1917 浏览 5 评论 0原文

我正在尝试用普通的旧 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

喜你已久 2025-01-15 06:17:20

您忘记初始化数组中的指针。尝试:

struct node {
    bool word;
    struct node *alpharray[26];
};

struct node *node_new(void)
{ 
struct node * ret;
unsigned uu;

ret = malloc (sizeof *ret);
if (!ret) return ret;

ret->word = false;
for (uu = 0; uu < 26 ; uu++) {
   ret->alpharray[uu] = NULL;
   }
return ret;
}

int main(void)
{
struct node *root , *nptr;
root = node_new();

for (nptr=root; (ch = fgetc( fp)) != EOF; ) {
     if (ch == '\n') break;
        //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)
        {
        nptr->alpharray[char_num] = node_new();
        }

    nptr = nptr->alpharray[char_num];
    }

}

You are forgetting to initialise the pointers in the array. Try:

struct node {
    bool word;
    struct node *alpharray[26];
};

struct node *node_new(void)
{ 
struct node * ret;
unsigned uu;

ret = malloc (sizeof *ret);
if (!ret) return ret;

ret->word = false;
for (uu = 0; uu < 26 ; uu++) {
   ret->alpharray[uu] = NULL;
   }
return ret;
}

int main(void)
{
struct node *root , *nptr;
root = node_new();

for (nptr=root; (ch = fgetc( fp)) != EOF; ) {
     if (ch == '\n') break;
        //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)
        {
        nptr->alpharray[char_num] = node_new();
        }

    nptr = nptr->alpharray[char_num];
    }

}
-黛色若梦 2025-01-15 06:17:20

如果将节点初始化为:

node *nptr = malloc(sizeof(node));

您为节点本身创建空间,但不是为每个指针创建空间,

struct node *alpharray[26];

因为 alphaarray 将为 NULL,因此您不能使用它直接获取它的元素。您也必须为这些元素进行 malloc。

If you initialize your node as :

node *nptr = malloc(sizeof(node));

You create space for the node itself, but not for each of the pointers in

struct node *alpharray[26];

Since alpharray will be NULL you cannot use it to get an element of it directly. You have to malloc for those elements too.

≈。彩虹 2025-01-15 06:17:20

你的“alphaarray”应该声明为:

struct node** alpharray[26];

因为在行中:

nptr->alpharray[char_num] = newnode;

你正在将指针分配给一个结构(这是非法的)。
您的节点声明应该是:

typedef struct node {
bool word;
struct node** alpharray[26];
} node;

当编译器看到您的“alphaarray”的(原始)声明时,它无法弄清楚
数组中每个元素的大小(因为类型尚未完全定义)。
这就是为什么除了指针数组之外它不能以任何其他方式工作(您所做的是“结构节点”数组,而不是指向“结构节点”的指针数组 - 您应该做什么)。

Your 'alpharray' should be declared as:

struct node** alpharray[26];

because in the line:

nptr->alpharray[char_num] = newnode;

you ares assigning pointer to a structure (which is illegal).
Your node declaration should be:

typedef struct node {
bool word;
struct node** alpharray[26];
} node;

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文