初始化初始化基本trie节点的不兼容指针类型

时间:2015-05-06 17:47:17

标签: c struct trie

我知道C对于文件级初始化非常有用。或者更确切地说,我只是不知道常量表达是什么意思。

我想要做的是用所有空指针初始化一个节点(也就是结构节点)。

//Trie node definition
typedef struct node{
    bool is_word;
    struct node* next[27]; //27 for the valid number of chars

}node;


struct node* empties[27];
node empty = {.is_word = 0, .next = empties};
dictionary.c:24:33: error: incompatible pointer types initializing 'struct node *' with an
      expression of type 'struct node *[27]' [-Werror,-Wincompatible-pointer-types]
node empty = {.is_word=0,.next =empties};
                                ^~~~~~~
dictionary.c:24:33: error: suggest braces around initialization of subobject
      [-Werror,-Wmissing-braces]
node empty = {.is_word=0,.next =empties};

我尝试初始化时遇到错误。我也会尝试手动初始化成员,但是27个索引使得这非常繁琐。有没有办法在文件级别循环初始化?

2 个答案:

答案 0 :(得分:1)

尝试node empty = {0, {0}};

这是初始化结构和数组的有效方法,或者在本例中是包含数组的结构。

How to initialize all members of an array to the same value?在阵列初始化方面有更多。但您也可以将初始化程序嵌入到结构中,如下所示。

答案 1 :(得分:0)

可以依赖永久变量(非自动,非动态)进行0初始化。但是,错误报告类型错误:next是一个指针数组,而您使用指向数组的指针初始化它。如果你真的想要一个指向数组的指针,请使用struct node (*next)[])。

因此第二条消息已经包含了一个提示,即你有一个嵌套的复合数据类型(struct中的数组)。请记住,每种复合类型的初始值设定项都需要用大括号括起来。