473,756 Members | 1,808 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.uni on1.field1 = 100;
unionMember.uni on1.field2 = 200 };
fullStructType var2[2] = { { unionMember.uni on2 = 300 } ,
{ unionMember.uni on2 = 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 10542
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.uni on1.field1 = 100;
unionMember.uni on1.field2 = 200 };
fullStructType var2[2] = { { unionMember.uni on2 = 300 } ,
{ unionMember.uni on2 = 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.u nion1 = {100, 200} };
fullStructType var2[2] = {[0].unionMember.un ion2 = 300,
[1].unionMember.un ion2 = 400
};
int main(void)
{
printf("var1.un ionMember.union 1.field1=%u\n",
var1.unionMembe r.union1.field1 );
printf("var1.un ionMember.union 1.field2=%u\n",
var1.unionMembe r.union1.field2 );
printf("var2[0].unionMember.un ion2=%u\n",
var2[0].unionMember.un ion2);
printf("var2[1].unionMember.un ion2=%u\n",
var2[1].unionMember.un ion2);

return 0;
}
[output]
var1.unionMembe r.union1.field1 =100
var1.unionMembe r.union1.field2 =200
var2[0].unionMember.un ion2=300
var2[1].unionMember.un ion2=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.uni on1.field1 = 100;
unionMember.uni on1.field2 = 200 };
fullStructType var2[2] = { { unionMember.uni on2 = 300 } ,
{ unionMember.uni on2 = 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.l earn.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
fullStructTy pe var1 = { unionMember.uni on1.field1 = 100;
unionMember.un ion1.field2 = 200 };
fullStructTy pe var2[2] = { { unionMember.uni on2 = 300 } ,
{ unionMember.uni on2 = 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.uni on1.field1 = 100;
unionMember.uni on1.field2 = 200 };
fullStructType var2[2] = { { unionMember.uni on2 = 300 } ,
{ unionMember.uni on2 = 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.un ion1.field1 =
100, .unionMember.un ion1.field2 = 200 };

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

easier approach would be

fullStructType var1 = { .unionMember.un ion1 = { 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
6723
by: Josh Lessard | last post by:
Given a union definition: union problem_t { int mask; struct { int indices; int ops; } comp; };
6
3334
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 gcc 3.2.2 but does not produce the expected results, the expected results being the ones annotated in the comments in the code): #include <stdlib.h>
2
5056
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 compiler said that the union has no name. Is it right that in ANSI C the union must be named inside this kind of structure ?
10
11388
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
2380
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 compiler), and it is, what I conclude from the below code union data_type {
5
1691
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 overwritten, i.e. should not be part of the union. The remaining 2, UnionData1 and UnionData2, make up the union and are populated as needed. I want to be certain that this code will work as planned. Specifically, if the field offsets of the...
18
12962
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
2338
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 initialization takes place." Does this mean all non-local objects will be zero-initialized before they are initialized by their initializers(if they have)? For example: int g_var = 3; int main() {}
27
2522
by: Nate Eldredge | last post by:
Consider the following pseudo-code: #include <opaque.h> struct foo { int a; opaque_t op; int b; };
0
9456
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9872
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9843
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8713
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, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.