Initializing Union Structure

B

benn686

I have a structure that contains a union that Id like to initialize at
compile time... something like:

//global declare and initialize
fullStructType var1 = { unionMember.union1.field1 = 100;
unionMember.union1.field2 = 200 };
fullStructType var2[2] = { { unionMember.union2 = 300 } ,
{ unionMember.union2 = 400 } };


where the structures are defined as :

typedef struct
{
union
{
fieldStructType union1;
alt_u16 union2;
} unionMember;

} fullStructType;

and

typedef struct{
alt_u8 field1;
alt_u8 field2;
} fieldStructType;


I get a bunch of errors on the variable declarations.. whats the
proper syntax??

Thanks!
 
M

Martin Ambuhl

I have a structure that contains a union that Id like to initialize at
compile time... something like:

//global declare and initialize
fullStructType var1 = { unionMember.union1.field1 = 100;
unionMember.union1.field2 = 200 };
fullStructType var2[2] = { { unionMember.union2 = 300 } ,
{ unionMember.union2 = 400 } };


where the structures are defined as :

typedef struct
{
union
{
fieldStructType union1;
alt_u16 union2;
} unionMember;

} fullStructType;

and

typedef struct{
alt_u8 field1;
alt_u8 field2;
} fieldStructType;


I get a bunch of errors on the variable declarations.. whats the
proper syntax??

You really should post your actual code. You have the first needed
typedef last, the second needed typedef before that, but after it is
used. And those alt_* types aren't any too useful. This sort of thing
makes it difficult to help. In any case, compare your effort to the
following:

#include <stdio.h>

typedef struct
{
unsigned char field1;
unsigned char field2;
} fieldStructType;

typedef struct
{
union
{
fieldStructType union1;
unsigned short union2;
} unionMember;

} fullStructType;


//global declare and initialize
fullStructType var1 = {.unionMember.union1 = {100, 200} };
fullStructType var2[2] = {[0].unionMember.union2 = 300,
[1].unionMember.union2 = 400
};


int main(void)
{
printf("var1.unionMember.union1.field1=%u\n",
var1.unionMember.union1.field1);
printf("var1.unionMember.union1.field2=%u\n",
var1.unionMember.union1.field2);
printf("var2[0].unionMember.union2=%u\n",
var2[0].unionMember.union2);
printf("var2[1].unionMember.union2=%u\n",
var2[1].unionMember.union2);

return 0;
}


[output]
var1.unionMember.union1.field1=100
var1.unionMember.union1.field2=200
var2[0].unionMember.union2=300
var2[1].unionMember.union2=400
 
J

Jack Klein

I have a structure that contains a union that Id like to initialize at
compile time... something like:

//global declare and initialize
fullStructType var1 = { unionMember.union1.field1 = 100;
unionMember.union1.field2 = 200 };
fullStructType var2[2] = { { unionMember.union2 = 300 } ,
{ unionMember.union2 = 400 } };


where the structures are defined as :

typedef struct
{
union
{
fieldStructType union1;
alt_u16 union2;
} unionMember;

} fullStructType;

and

typedef struct{
alt_u8 field1;
alt_u8 field2;
} fieldStructType;

There isn't any way to initialize all the members of a union. And it
wouldn't make sense if you did. A union is a type which has two or
more members starting at the same address. In general, only the last
member written to has a valid value. Writing that value overwrites
some or all of the bits in any other members, basically rendering
their previous contents invalid.
I get a bunch of errors on the variable declarations.. whats the
proper syntax??

C prior to 1999 allows you to initialize the first defined member of a
union, only. If you are using a compiler that conforms to the 1999 or
later version of the standard, there is a feature called designated
initializer, but that still doesn't do what you want by initializing
multiple members of a union. The last initializer overwrites previous
values.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
A

Army1987

I have a structure that contains a union that Id like to initialize at
compile time... something like:

//global declare and initialize
fullStructType var1 = { unionMember.union1.field1 = 100;
unionMember.union1.field2 = 200 };
fullStructType var2[2] = { { unionMember.union2 = 300 } ,
{ unionMember.union2 = 400 } };


where the structures are defined as :

typedef struct
{
union
{
fieldStructType union1;
alt_u16 union2;
} unionMember;

} fullStructType;

and

typedef struct{
alt_u8 field1;
alt_u8 field2;
} fieldStructType;

There isn't any way to initialize all the members of a union.
That is not what the OP was trying to do. Re-read the definitions.
In var he's trying to initialize both members of the first field
(union1, whose type is fieldStructType) of the unionMember member
of the struct. In var2[0] he's trying to initialize the second
member of the union, and in var2[1] too.
 
C

ciju

I have a structure that contains a union that Id like to initialize at
compile time... something like:

//global declare and initialize
fullStructType var1 = { unionMember.union1.field1 = 100;
unionMember.union1.field2 = 200 };
fullStructType var2[2] = { { unionMember.union2 = 300 } ,
{ unionMember.union2 = 400 } };

where the structures are defined as :

typedef struct
{
union
{
fieldStructType union1;
alt_u16 union2;
} unionMember;

} fullStructType;

and

typedef struct{
alt_u8 field1;
alt_u8 field2;

} fieldStructType;

I get a bunch of errors on the variable declarations.. whats the
proper syntax??

Thanks!

hey benn,

definition of fieldStructType struct type should be visible to
fullStructType struct type.
so i hope the typedef for fieldStructType is before fullStructType.

after that, the initialization for var1 needs to be changed to

fullStructType var1 = { .unionMember.union1.field1 =
100, .unionMember.union1.field2 = 200 };

the difference being, "." before the member reference, and the ","
between the member initializers.

easier approach would be

fullStructType var1 = { .unionMember.union1 = { 100, 200} };

better/worser would be

fullStructType var1 = { { { 100, 20 } } } ;

or even

fullStructType var1 = { 100, 20 } ;

by default initialization is done to the first union member(c standard
stuff). thats why the later 2 approaches work.
and the braces r just for our convenience or when we r not
initializing all members of a struct/nested array etc(braces could
even be skipped if only starting members of struct/array.. need to be
initialized.

ciju
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top