I can't remember ...
What does the union { ... } do?

How is it used???
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

a union is a vay of having more than one data type reside in the same memory structure.  For instance, say you want to be able to store an array of "things".  Some of these are char, some are int, some are floats.  A union can permit this:

union unionMultiType {
  char ch;
  int i;
  float fl;

Now you can use it:

union unionMultiType myunion;

you can access the different parts of the union like a struct:

myunion.ch gets your the char part.  Since the union allocates memory based on the largest type that needs to fit in it, the size depends on what you put in it.  Remember, however, that unlike a struct, all of the possible types in a union take up the same "space" so there is only room for one of the types at a time.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lar_jensAuthor Commented:
I remember now..
>> more than one data type reside in the same
>> memory structure.
I think he means "same memory location"

Except in rare instances, like when dealing with low level stuff, like hardware or the OS, there is no place for unions in C++.  In general, avoid unions at all costs.  The have many potential problems, like they don't work well with classes that have cosntructors.
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.


Maybe you should talk with Microsoft about their practices.

COM uses unions, it's not low level, nor is it hardware or OS specific....


typedef struct tagVARIANT  {
    VARTYPE vt;
    unsigned short wReserved1;
    unsigned short wReserved2;
    unsigned short wReserved3;
    union {
        Byte                        bVal;                        // VT_UI1.
        Short                        iVal;                        // VT_I2.
        long                        lVal;                        // VT_I4.
        float                        fltVal;                    // VT_R4.
        double                    dblVal;                    // VT_R8.
        VARIANT_BOOL            boolVal;                    // VT_BOOL.
        SCODE                        scode;                    // VT_ERROR.
        CY                            cyVal;                    // VT_CY.
        DATE                        date;                        // VT_DATE.
        BSTR                        bstrVal;                    // VT_BSTR.
        DECIMAL                    FAR* pdecVal            // VT_BYREF|VT_DECIMAL.
        IUnknown                    FAR* punkVal;            // VT_UNKNOWN.
        IDispatch                 FAR* pdispVal;            // VT_DISPATCH.
        SAFEARRAY                FAR* parray;            // VT_ARRAY|*.
        Byte                        FAR* pbVal;                // VT_BYREF|VT_UI1.
        short                        FAR* piVal;                // VT_BYREF|VT_I2.
        long                        FAR* plVal;                // VT_BYREF|VT_I4.
        float                        FAR* pfltVal;            // VT_BYREF|VT_R4.
        double                    FAR* pdblVal;            // VT_BYREF|VT_R8.
        VARIANT_BOOL            FAR* pboolVal;            // VT_BYREF|VT_BOOL.
        SCODE                        FAR* pscode;            // VT_BYREF|VT_ERROR.
        CY                            FAR* pcyVal;            // VT_BYREF|VT_CY.
        DATE                        FAR* pdate;                // VT_BYREF|VT_DATE.
        BSTR                        FAR* pbstrVal;            // VT_BYREF|VT_BSTR.
        IUnknown                 FAR* FAR* ppunkVal;    // VT_BYREF|VT_UNKNOWN.
        IDispatch                 FAR* FAR* ppdispVal;// VT_BYREF|VT_DISPATCH.
        SAFEARRAY                 FAR* FAR* pparray;    // VT_ARRAY|*.
        VARIANT                    FAR* pvarVal;            // VT_BYREF|VT_VARIANT.
        void                            FAR* byref;                    // Generic ByRef.
char                            cVal;                            // VT_I1.
unsigned short                uiVal;                        // VT_UI2.
unsigned long                ulVal;                        // VT_UI4.
int                            intVal;                        // VT_INT.
unsigned int                uintVal;                        // VT_UINT.
char FAR *                    pcVal;                        // VT_BYREF|VT_I1.
unsigned short FAR *    puiVal;                        // VT_BYREF|VT_UI2.
unsigned long FAR *        pulVal;                        // VT_BYREF|VT_UI4.
int FAR *                    pintVal;                        // VT_BYREF|VT_INT.
unsigned int FAR *        puintVal;                    //VT_BYREF|VT_UINT.
lar_jensAuthor Commented:
I just wondered what the union inside
struct in_addr was for.. Now I know..
I gives you the opprtunity to manipulate
an ip-address as :

4 bytes (8bit)
2 words (16bit) or
one long word (32bit)..

I excuse the terms here, but they've
been there ever since I started
assembly programming on Amiga.. =)
In this case, it's just 3 different ways of looking at the same data.

An IP address is a 32-bit number.  Sometimes it's convenient to look at it as a 32-bit value.  Other times, it's more convenient to use the 4 8-bit values, like with  That's 4 byte-wide values displayed in DECIMAL numbers.

I've not had a use for the 2 16-bit words but I'm sure someone has.
lar_jensAuthor Commented:
Could it be MicroSoft??
>> Maybe you should talk with Microsoft
>> about their practices.
Thats why I said OS.  OSs have a tendancy to use union types to save spae.  unfortunately they tend to cause a lot of headaches for less experienced programmers.  Especially in an OOP languages.  
lar_jensAuthor Commented:
I'm sorry, but the comment was ment as
a joke, NOT to be taken seriously...

It was also a comment about using
and IP adress as two 16bit values,
not about using union..

Keep up the good work everyone..
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.