Flexible array member initializers
Jonathan Wakely
jwakely.gcc@gmail.com
Wed Jan 8 08:59:00 GMT 2014
On Jan 8, 2014 5:13 AM, "Ian Pilcher" wrote:
>
> Note the use of the flexible array member to hold the default value (or
> possible a pointer to the default value, depending on the type.)
>
> I'm trying to figure out if there is any way to statically initialize a
> struct foo_opt with a default value that is not an array of unsigned
> characters. For example:
>
> struct foo_type foo_int = {
> .name = "integer",
> .parse_fn = foo_parse_int,
> .format_fn = foo_format_int,
> .free_fn = 0,
> .size = sizeof(int),
> };
>
> struct foo_opt foo_option = {
> .name = "max_workers",
> .type = &foo_int,
> .flags = FOO_OPT_HAS_DEFAULT,
> .def_val = (unsigned char[])5,
> };
>
> My goal is to provide some sort of macro which will make it (reasonably)
> simple to provide static initializers for the foo_opt structs -- without
> replacing the flexible array member with a void *.
>
> Is there any way to do this?
No. That's not how flexible array members work.
The member def_value has no storage, so you can't initialize it. To
use it you need to allocate sizeof(struct foo_opt)+sizeof(x) where x
is the value you want def_val to have, and when you access def_val you
will be accessing the extra sizeof(x) bytes after the struct.
I don't see how you can statically initialize a foo_opt when you don't
know sizeof(x).
More information about the Gcc-help
mailing list