473,545 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Setting union member in structure

I am having a syntax issue that I hope someone else here knows how to
rectify...

I am loading an INI file and have a simple function to load values from
it. The function is overloaded with the different return types int,
double or string. A structure is defined:

typedef struct INI_KEY {
char strCategory[32];
char strKey[32];

union {
int iDefaultValue;
double dDefaultValue;
char strDefaultValue[32];
} def;
} INI_KEY;

In the implementation is a global array of INI_KEY's that list off
various categories, keys and their default value (the value to return if
it isn't in the INI):

const INI_KEY g_keys[] = {
{ "category", "keyInt", 0 },
{ "category", "keyDouble" , 0.0 },
{ "category", "keyString" , "foo" },
// ...
};

The problem I'm having is in the default value of the global array. The
compiler is trying to convert the double and string values to integers
and generating compiler errors. I'm sure there is a syntax to tell the
compiler what member of the union to use, I just don't know it. If it
makes a difference, I'm using MS VC++ 7.

--
Best regards,
Jeff jma[at]mfire.com
http://www.jm-basic.com
Jul 22 '05 #1
2 2511

"Jeff Massung" <jm*@NOSPAM.mfi re.com> wrote in message
news:vu******** ****@corp.super news.com...
I am having a syntax issue that I hope someone else here knows how to
rectify...

I am loading an INI file and have a simple function to load values from
it. The function is overloaded with the different return types int,
double or string. A structure is defined:

typedef struct INI_KEY {
char strCategory[32];
char strKey[32];

union {
int iDefaultValue;
double dDefaultValue;
char strDefaultValue[32];
} def;
} INI_KEY;

In the implementation is a global array of INI_KEY's that list off
various categories, keys and their default value (the value to return if
it isn't in the INI):

const INI_KEY g_keys[] = {
{ "category", "keyInt", 0 },
{ "category", "keyDouble" , 0.0 },
{ "category", "keyString" , "foo" },
// ...
};

The problem I'm having is in the default value of the global array. The
compiler is trying to convert the double and string values to integers
and generating compiler errors. I'm sure there is a syntax to tell the
compiler what member of the union to use,
There is not.
I just don't know it. If it
makes a difference, I'm using MS VC++ 7.


=============== =============== =============== =============== =========
ISO/IEC 14882:1998(E)

8.5.1 Aggregates

[....]

15 When a union is initialized with a brace*-enclosed initializer,
the braces shall only contain an initializer for the first member
of the union. [Example:

union u { int a; char* b; };
u a = { 1 };
u b = a;
u c = 1; // error
u d = { 0, "asdf" }; // error
u e = { "asdf" }; // error

--end example] [Note: as described above, the braces around the
initializer for a union member can be omitted if the union is
a member of another aggregate. ]
=============== =============== =============== =============== =========

-Mike
Jul 22 '05 #2
"Mike Wahler" <mk******@mkwah ler.net> wrote in
news:c5******** ********@newsre ad2.news.pas.ea rthlink.net:

"Jeff Massung" <jm*@NOSPAM.mfi re.com> wrote in message
news:vu******** ****@corp.super news.com...

const INI_KEY g_keys[] = {
{ "category", "keyInt", 0 },
{ "category", "keyDouble" , 0.0 },
{ "category", "keyString" , "foo" },
// ...
};

The problem I'm having is in the default value of the global array.
The compiler is trying to convert the double and string values to
integers and generating compiler errors. I'm sure there is a syntax
to tell the compiler what member of the union to use,


There is not.

=============== =============== =============== =============== =========
ISO/IEC 14882:1998(E)

8.5.1 Aggregates

[....]

15 When a union is initialized with a brace*-enclosed initializer,
the braces shall only contain an initializer for the first member
of the union. [Example:

union u { int a; char* b; };
u a = { 1 };
u b = a;
u c = 1; // error
u d = { 0, "asdf" }; // error
u e = { "asdf" }; // error

--end example] [Note: as described above, the braces around the
initializer for a union member can be omitted if the union is
a member of another aggregate. ]
=============== =============== =============== =============== =========

-Mike


Oh well. Thanks for the quick reply.

--
Best regards,
Jeff jma[at]mfire.com
http://www.jm-basic.com
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3321
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...
2
5041
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 ?
18
2367
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 {
18
12940
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;
8
5644
by: wkaras | last post by:
In my compiler, the following code generates an error: union U { int i; double d; }; U u; int *ip = &u.i; U *up = static_cast<U *>(ip); // error I have to change the cast to reinterpret_cast for the code
30
3228
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
9
5694
by: dspfun | last post by:
Hi! I have stumbled on the following C question and I am wondering wether the answer is implementation specific or if the answer is always valid, especially question b). BRs! ---------------------------------------------------- Following C code runs on a "little endian" machine that has a 32-bit
14
28086
by: deepak | last post by:
Hi Experts, I'm getting this compilation error while trying to access a member in structure. at what time we will get this error message? Thanks, Deepak
5
6371
by: hnshashi | last post by:
I have writtem kernel(2.4) module to commu. with user space appl. using netlink socket. I am getting compilation error. kernel module:-> #include <linux/skbuff.h> #include<linux/module.h> #include <linux/socket.h> #include <linux/config.h> #include <linux/module.h> #include <linux/kernel.h>
0
7502
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7434
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7692
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7457
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...
0
7791
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...
0
6026
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 project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5078
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...
0
3491
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...
0
744
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.