[英]Error when initializing struct members in struct definition
I am trying to create a struct that has 2 pointers of type struct nodo
defined above the code.我正在尝试创建一个结构,该结构具有 2 个在代码上
struct nodo
类型的指针。 But it gives me the error:但它给了我错误:
expected ':', ',', ';', '}' or 'attribute' before '=' token lista dati_mappa=NULL;".
在 '=' token lista dati_mappa=NULL; 之前应为 ':', ',', ';', '}' 或 'attribute'。
Here is the code:这是代码:
typedef int data;
struct nodo
{
data elem;
struct nodo *next;
};
typedef struct nodo *lista;
struct mappa {
data R;
data C;
lista dati_mappa = NULL;
lista pos_valida = NULL;
};
You're not allowed to have inline initialization of structure members in C.不允许在 C 中对结构成员进行内联初始化。
If you want to initialize members then you need to do it after you create instances of the structures.如果要初始化成员,则需要在创建结构实例后进行。
You can't do that at declaration level in C (in C++ you can).您不能在 C 中的声明级别执行此操作(在 C++ 中可以)。
You need something like this:你需要这样的东西:
...
int main()
{
struct mappa mymap;
mymap.dati_mappa = NULL;
mymap.pos_valida = NULL;
...
}
or you can use aggragate initialization:或者您可以使用聚合初始化:
...
int main()
{
struct mappa mymap = { .dati_mappa = NULL,
.pos_valida = NULL };
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.