简体   繁体   中英

Initialization of an array in a structure

I have declared 2 structure :

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:

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.

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}};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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