On 19 July 2011 23:57, Cheng Renquan wrote:
> On Tue, Jul 19, 2011 at 2:56 PM, Cheng Renquan <[email protected]> wrote:
>> Hi all,
>>
>> From info gcc I know it accepts a series of `.FIELDNAME' and `[INDEX]'
>> designators,
>> like
>>
>> struct point ptarray[10] = { [2].y = yv2, [2].x = xv2, [0].x = xv0 };
>>
>>
>> But in my case, I have a struct with array of int as members,
>>
>> struct mbox {
>> int x[20];
>> int y[20];
>> };
>>
>> and want to declare a mbox variable with partially initialized, like
>>
>> struct mbox mbox = { .x = { 1, 2 }, .y[19] = 3, };
>
> Sorry, my above example is not good, it compiles and works ok;
>
> What's really not working is this example by union:
>
> union mbox {
> int w[3];
> short s[6];
> char c[12];
> };
>
> If I declare a union mbox and want to initialize w[0], s[2] and c[6],
>
> union mbox mbox = { .w = { 1, }, .s[2] = 2, .c[6] = 's' };
>
> I want a mbox initialized with these 12 bytes (on little endian):
>
> 0000: 01 00 00 00 02 00 73 00 00 00 00 00
>
> And actually I got these:
>
> 0000: 00 00 00 00 00 00 73 00 00 00 00 00
>
> the hexdump result shows only last .c[6] make effect,
>
> Wonder if this is a bug or not supported? Thanks,
>
This question is more suitable for the gcc-help list, as it is a
question about using gcc not about developing it.
What you want is not supported. The member of the union that is
initialized will either be the int[3], or the short[6], or the
char[12]. You cannot initialize some bytes of one member and some
bytes of another like that.
Maybe you should just do this instead:
union mbox mbox = { .c[0] = 1, .c[4] = 2, .c[6] = 's' };