c++ - What's the proper format for a union initializer list? -


say have class/struct single union in it... like:

struct box { union { as128 intr; struct { as32 a, b, c, d; }; }; }; 

what's proper way initializer list kind of data type?

box imyboxa = { 10, 10, 100, 100 }; box imyboxb = ( box ){ 10, 10, 100, 100 }; 

the 'a' option above works in many cases, isn't portable... giving error "no known conversion argument 1 'brace-enclosed initializer list'". , second doesn't compile "cannot omit braces around initialization of subobject..."

i've tried few other cases , can't seem compile across multiple platforms.

the original version of structure , union definition in question was:

struct box { union { as128 intr; as32 a, b, c, d; } }; 

this little unusual. assuming types as128 , as32 defined, equivalent to:

struct box1  // renamed convenience of testing {     union     {         as128 intr;         as32  a;         as32  b;         as32  c;         as32  d;     }; }; 

that is, union of as128 called intr , 4 separate values of type as32 called a, b, c , d (with latter 4 occupying same space).

presumably, have in mind is:

struct box2 {     union     {         as128 intr;         struct         {             as32  a;             as32  b;             as32  c;             as32  d;         } s;     }; }; 

this uses anonymous members feature of c11 (listed in foreword of iso/iec 9899:2011 new feature compared c99). [the question has been fixed reflect more or less notation (using anonymous structure). note can't directly designate anonymous union member, s above idea, @ least. structure without s can used, don't think can initialize structure unless can designate — can designate named elements of union.]

with first variant (struct box1), can initialize first member (the as128 intr; member) no name:

struct box1 b1 = { { 1234 } }; 

the outer braces structure; inner braces (anonymous) union.

or can specify member initialize using designated initializer:

struct box1 b2 = { { .a = 1234 } }; 

note can't do:

struct box1 b3 = { { .a = 1234, .b = 2341 } }; 

the compiler gives warning (see example code below).

with second variant (struct box2), nested structure, can still use undesignated initializer or designated initializers, time, specify 4 of members of structure:

struct box2 b4 = { { .s = { .a = 32, .b = 65, .c = 48, .d = 97 } } }; 

example code

using funny types as128 , as32 type names.

typedef long as128; typedef char as32;  struct box1 {     union     {         as128 intr;         as32  a;         as32  b;         as32  c;         as32  d;     }; };  struct box2 {     union     {         as128 intr;         struct         {             as32  a;             as32  b;             as32  c;             as32  d;         } s;     }; };  struct box1 b1a = { { 1234 } }; struct box1 b1b = { { .intr = 1234 } }; struct box1 b1c = { { .a = 32 } }; //struct box1 b1d = { { .b = 29, .c = 31 } }; // invalid double initialization //error: initialized field overwritten [-werror=override-init]  struct box2 b2a = { { 1234 } }; struct box2 b2b = { { .intr = 1234 } }; struct box2 b2c = { { .s = { .a = 29, .b = 30, .c = 31, .d = 32 } } }; 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -