I have my struct like below , there can be n number of vendor which can contain n number of test struct.
I am trying to initialize this structure . This is a sample code I am trying , later I want to make it using macros and load the structure like X-macros.
I am also using flexible structure concept as I do not know how many test structs for a vendor are going to be. The data would be in a file , the struct needs to load all that is there. I have created a minimal sample code for SO. Below is my code.
#include <stdio.h>
typedef struct test{
int a;
int b;
int c;
}test;
typedef struct vendor{
int size;
test t[0];
}vendor;
vendor v[]={
{.size = 1, .t[] = {{1,2,3},}}
};
int main()
{
return 0;
}
I get this error -
a.c:16: error: expected expression before ‘]’ token
a.c:16: error: array index in initializer not of integer type
a.c:16: error: (near initialization for ‘v[0].t’)
a.c:16: error: extra brace group at end of initializer
a.c:16: error: (near initialization for ‘v[0]’)
a.c:16: error: extra brace group at end of initializer
a.c:16: error: (near initialization for ‘v[0]’)
a.c:16: warning: excess elements in struct initializer
a.c:16: warning: (near initialization for ‘v[0]’)
I have tried without flexible struct , no luck so far. any suggestions on how to init this struct ?
[]instead of[0]in the structure. But you then can't statically allocate an array of structures containing a FAM — again in standard C. GCC has extensions that override the rules of the standard; these are not necessarily good features to use. With an ordinary array member, you'd use.t = { { 1, 2, 3 }, … };to provide the initializers. Standard C says you can't provide an initializer for a structure with a FAM.