Flexible array member initializers
Jonathan Wakely
jwakely.gcc@gmail.com
Wed Jan 8 21:43:00 GMT 2014
On 8 January 2014 20:41, Ian Pilcher wrote:
> On 01/08/2014 03:59 AM, Jonathan Wakely wrote:
>>
>> 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).
>>
>
> Here's a very simplified example for the specific case of a 16-bit short
> on a little-endian system:
>
> #define SHORT_BYTES(s) { ((s) & 0xff), (((s) >> 8) & 0xff) }
>
> struct foo {
> size_t value_size;
> unsigned char value[];
> };
>
> static struct foo foo_short = {
> .value_size = sizeof(short),
> .value = SHORT_BYTES(513),
> };
Where do you think those two bytes are meant to be stored?
More information about the Gcc-help
mailing list