In C, I have defined the struct
seen below, and would like to initialize it inline. Neither the fields inside the struct, nor the array foos
will change after initialization. The code in the first block works fine.
struct Foo {
int bar;
int *some_array;
};
typedef struct Foo Foo;
int tmp[] = {11, 22, 33};
struct Foo foos[] = { {123, tmp} };
However, I don't really need the tmp
field. In fact, it will just clutter my code (this example is somewhat simplified). So, instead I'd like to declare the values of some_array
inside the declaration for foos
. I cannot get the right syntax, though. Maybe the field some_array
should be defined differently?
int tmp[] = {11, 22, 33};
struct Foo foos[] = {
{123, tmp}, // works
{222, {11, 22, 33}}, // doesn't compile
{222, new int[]{11, 22, 33}}, // doesn't compile
{222, (int*){11, 22, 33}}, // doesn't compile
{222, (int[]){11, 22, 33}}, // compiles, wrong values in array
};