fedi
fedi

Reputation: 468

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

Upvotes: 4

Views: 57

Answers (1)

Lundin
Lundin

Reputation: 213276

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

Upvotes: 1

Related Questions