Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

When pre-defining a structure using a union, how to tell compiler what element to place the value into?

Consider this code:

>struct MyStruct
>{
>    union
>    {
>        int i;
>        float f;
>    } value;
>};
>
>
>MyStruct Values[] =
>{
>    300L,
>    10.0f,
>    15.0f,
>}

Now the 10.0f, and the 15.0f values are converted to integer, and placed into the union. How, if possible, to make them remain as floats and then placed into the union. I tried using a void * in the union as the first field, but the compiler wont convert floats to void *.
0
Vectre
Asked:
Vectre
  • 2
1 Solution
 
n_fortynineCommented:
check this out (the anonymous union part)
http://www.cplusplus.com/doc/tutorial/tut3-6.html
0
 
VectreAuthor Commented:
Anonymous union or not, the values still are converted to the first type in the union.

0
 
AlexFMCommented:
struct MyStruct
{
    MyStruct(float f)
    {
        value.f = f;
    }

    MyStruct(long i)
    {
        value.i = i;
    }

    union
    {
        int i;
        float f;
    } value;
};
0
 
VectreAuthor Commented:
Guess one's screwed in regular C eh?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now