I'm trying to declare some constant data structures in my code, ideally without having to declare subelements (this is a follow-on from a previous question about multiple lists of strings: different length arrays) ... as per my previous question I can get it to work by declaring bits individually, but can't declare it all in one go.
I should add, I need a pointer to the data structure (the pointer will be in a different linker section so I can discover them automatically)...
This works...
struct cpl {
int x;
char *a;
char *b;
char *list[];
};
const struct cpl one = { 1, "thisa", "thisb", { "one", "two", NULL }};
const struct cpl *xx = &one;
But this generates an error about non-static initialisation of a flexible array member..
const struct cpl *xx = &(const struct cpl){ 2, "thisa", "thisb", { "new", "item", "here", NULL } };
I suspect the answer (assuming it is possible) is similar to before, by adding some kind of casting to it, but I've tried everything I can think of.
In this case I probably can get by with the first version, but I'd rather not if I can avoid it.
Any help appreciated.
#include <stdlib.h>
or something else to defineNULL
. (b) WhenNULL
is defined, and the structure definition is included with the second piece of code, GCC 13.2 compiles each piece of code without complaint. Possibly you are using the code inside a function, which changes the semantics.static
in the compound literal.