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), }; Ideally I could create some sort of generic macro that "casts" (using the term very loosely) any static initializer to an array of unsigned char that contains its binary representation. Sadly, I don't think that's actually possible. I don't supposed that gcc provides any facility that will allow the pre-processor to determine endianness and/or type sizes? -- ======================================================================== Ian Pilcher arequipeno@xxxxxxxxx "If you're going to shift my paradigm ... at least buy me dinner first." ========================================================================