473,405 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 10492
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Josh Lessard | last post by:
Given a union definition: union problem_t { int mask; struct { int indices; int ops; } comp; };
6
by: Neil Zanella | last post by:
Hello, I would like to know what the C standards (and in particular the C99 standard) have to say about union initializers with regards to the following code snippet (which compiles fine under...
2
by: Peter Dunker | last post by:
Hi, I will write ANSI C89. Is the following struct defenition correct ? I wrote it with VC6(Windows IDE) and at first no Problem. As I changed a compiler switch to 'no language extension', the...
10
by: tapeesh | last post by:
I created a C file say struct.c with the following structure declarations in the same file struct A { union key { int i; float f; }k1;
18
by: ranjeet.gupta | last post by:
Dear ALL As we know that when we declare the union then we have the size of the union which is the size of the highest data type as in the below case the size should be 4 (For my case and...
5
by: Mike | last post by:
Within the following structure, TopStruct, I'd like to create 3 other structures, 2 of which make up a union. The first structure will always contain some data that I need and should never be...
18
by: Mockey Chen | last post by:
My friend ask me a question as following: give a union SU define as: typedef union _SU { short x; struct y{ char a; short b; char c;
13
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
27
by: Nate Eldredge | last post by:
Consider the following pseudo-code: #include <opaque.h> struct foo { int a; opaque_t op; int b; };
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.