简体   繁体   English

在结构中初始化数组

[英]Initialization of an array in a structure

I have declared 2 structure : 我宣布了2个结构:

typedef struct
{
  int a;
  int b;
}ma_Struct;

typedef struct
{
  int x;
  ma_Struct tab[2];
}maStruct_2;

The goal is to initialise an instance of maStruct_2 so what i did is: 目标是初始化maStruct_2的实例,所以我做的是:

int main()
{
 ma_Struct elm1={0,1};
 ma_Struct elm2={1,2};

 ma_Struct tab_Elm[2]={elm1,elm2};
 maStruct_2 maStruct_2_Instance={1,tab_Elm};

return 0;
}

but i got the warning missing braces around initializer,i tried this syntax 但我得到了初始化器周围缺少括号的警告,我尝试了这种语法

maStruct_2 maStruct_2_Instance={1,{tab_Elm}};

but the same warning appears. 但同样的警告出现了。 Could you please help me 请你帮助我好吗

In C, you cannot initialize an array by using another array name as initializer. 在C中,您不能使用另一个数组名称作为初始化程序来初始化数组。

So the error has nothing to do with structs as such, nor does it have anything to do with scope or constant expressions. 因此,错误与结构本身无关,也与范围或常量表达式无关。

Fix your code like this: 像这样修复你的代码:

maStruct_2 maStruct_2_Instance = {1, {elm1, elm2}};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM