469,964 Members | 1,814 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,964 developers. It's quick & easy.

Initializing Union Structure

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!

Sep 17 '07 #1
4 10056
be*****@hotmail.com wrote:
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
Sep 18 '07 #2
On Mon, 17 Sep 2007 22:08:12 -0000, be*****@hotmail.com wrote in
comp.lang.c:
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
Sep 19 '07 #3
On Tue, 18 Sep 2007 21:47:39 -0500, Jack Klein wrote:
On Mon, 17 Sep 2007 22:08:12 -0000, be*****@hotmail.com wrote in
comp.lang.c:
>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.
--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen

Sep 19 '07 #4
On Sep 18, 3:08 am, benn...@hotmail.com wrote:
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

Sep 28 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Josh Lessard | last post: by
6 posts views Thread by Neil Zanella | last post: by
2 posts views Thread by Peter Dunker | last post: by
10 posts views Thread by tapeesh | last post: by
18 posts views Thread by ranjeet.gupta | last post: by
5 posts views Thread by Mike | last post: by
18 posts views Thread by Mockey Chen | last post: by
13 posts views Thread by WaterWalk | last post: by
27 posts views Thread by Nate Eldredge | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.