Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Setting union member in structure

Reply
Thread Tools

Setting union member in structure

 
 
Jeff Massung
Guest
Posts: n/a
 
      12-22-2003
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
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      12-22-2003

"Jeff Massung" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Jeff Massung
Guest
Posts: n/a
 
      12-22-2003
"Mike Wahler" <(E-Mail Removed)> wrote in
news:c5GFb.1651$(E-Mail Removed) nk.net:

>
> "Jeff Massung" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>
>> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
request for member in something not a structure or union deepak C Programming 12 05-09-2008 09:24 PM
Difference between Structure & Union csudha C Programming 4 09-16-2004 12:57 PM
union in struct without union name Peter Dunker C Programming 2 04-26-2004 07:23 PM
map XML union to C union (and vice-versa) Matt Garman XML 1 04-25-2004 12:40 AM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM



Advertisments