I would like to initialize a NULL terminated array of pointer to struct using compound literals.
Currently, I achieve this by:
struct object
{
int data1;
char data2;
};
struct object *object_array[] =
{
&(struct object) {1, 2},
&(struct object) {3, 4},
&(struct object) {5, 6},
&(struct object) {7, 8},
&(struct object) {9, 10},
NULL,
};
But, specifying (struct object) is very verbose.
Is there a way to make my code look something like this:
struct object
{
int data1;
char data2;
};
struct object *object_array[] =
{
&{1, 2},
&{3, 4},
&{5, 6},
&{7, 8},
&{9, 10},
NULL,
};
&when you actually need to pass a pointer?NULLpointer terminated? Are you using an external third-party API that requires pointers andNULLpointer termination? Can't there be a special termination object (where both values in the structure are something they can't be otherwise)? Can't you pass the number of elements (easily calculated) to the functions that need to use the array? There's a lot of information left out of the question.