?
Solved

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

Posted on 2003-03-16
4
Medium Priority
?
231 Views
Last Modified: 2010-04-01
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
Comment
Question by:Vectre
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 4

Expert Comment

by:n_fortynine
ID: 8150107
check this out (the anonymous union part)
http://www.cplusplus.com/doc/tutorial/tut3-6.html
0
 

Author Comment

by:Vectre
ID: 8150149
Anonymous union or not, the values still are converted to the first type in the union.

0
 
LVL 48

Accepted Solution

by:
AlexFM earned 400 total points
ID: 8150166
struct MyStruct
{
    MyStruct(float f)
    {
        value.f = f;
    }

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

    union
    {
        int i;
        float f;
    } value;
};
0
 

Author Comment

by:Vectre
ID: 8150270
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses
Course of the Month11 days, 15 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question