It is possible to do something like this How can I initialize an array of pointers to structs?
but with different structs?
E.g.
static struct structA_t a = {"ads", "as"};
static struct structB_t b = {"zzds", "dfr", "shywsd"};
static struct structC_t c = {"ssa", "ad", "dhksdhs"};
struct some_type *array[] = { &a, &b, &c};
How some_type will look like?
You could define
some_typeas a union:This will lead you to the problem that you don’t know what’s actually contained in which element in the array.
To overcome this, add another field specifying the content that is used:
Then you could initialize your array as follows:
When you loop over the elements of
array, first evaluatedataIdso that you know what’s contained inmyData. Then, for example, access the data of the first element usingor the third element with
See this ideone for a working example: http://ideone.com/fcjuR