Initializing an array in a structure

I declared 2 structures:

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

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

The goal is to initialize the maStruct_2 instance, so I did:

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 a warning about missing initializer bindings, I tried this syntax

maStruct_2 maStruct_2_Instance={1,{tab_Elm}};

but the same warning appears. Could you help me?

+4
source share
1 answer

In C, you cannot initialize an array using a different array name as an initializer.

Thus, the error has nothing to do with structures as such and has nothing to do with scope or constant expressions.

Correct your code as follows:

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

Source: https://habr.com/ru/post/1606205/


All Articles