Link to home
Start Free TrialLog in
Avatar of rstone
rstone

asked on

Could someone explain "union" to me?

I don't understand what a "union" is.  I've learned my C from C for Dummies, and a little bit of experience, but I've never come across "union".  Here's the code I came across in an application.  Could someone please explain to me what's going on in this code?

typedef struct
{
char ByteSize;

union
{
    struct
    {
    char Data[1];
    } SmallItem;

    struct
    {
    int WordSize;
    char Data[1];
    } LargeItem;
} u;

} DB_ITEM;

The DB_ITEM structure holds a variety of data.  I don't know if all of the data is stored as a string, or if it can somehow store both strings and integers and floats.

I just need an explanation of what's happening in this structure.
ASKER CERTIFIED SOLUTION
Avatar of viktornet
viktornet
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rstone
rstone

ASKER

So what's happening in my structure?  The first variable (ByteSize) is a char.  The second variable (u) is a structure that has either a one character string, or an integer followed by a one character string?  Is that right?

let's say you have three variables in your union...

the first and second variables' sizes are one byte... the third variable's size is two bytes...

now let's say you use MyUnion.ThirdVar = value;

and set the third variable whose size is two bytes..

now if you output variable1 it will output half the variable, and if you poutput the second variable it will output the other half of the variable..  those halves are called upper and lower bytes.. (note sure if that's what they were called ;-))

let's say the hex value of the 2 byte variable (3rd one) is 1234h (h represents hexadecimal)

MyUnion.ThirdVar = 1234h

now..

MyUnion.FirstVar = 12h
MyUnion.SecondVar = 34h

do you understand how this works?

..-=ViKtOr=-..
This may help a little:

typedef union test
  {
  char a;
  int  b;
  long c;
  } test;

test aVar;

/* Now, you can assign a character to aVar.a, an integer to aVar.b, and a long to aVar.c, BUT you can only assign one value at a time!  So, if you assign a character to aVar.a, and then assign an integer to aVar.b, the data in aVar.a may be overwritten.  
It is up to the user to keep track of what is in the union at any given time.  Unions can be useful to save space, but allocating only what is needed, and they can also be used to mimic templates.  */

Why on earth did you give me a C?
Avatar of rstone

ASKER

You gave me a textbook answer, which was fine.  It enabled me to understand what a union is.  However, I had asked for an explanation of my particular structure and union, and you didn't provide it.  I was able to figure out some of my information on my own, but it took me longer than if you had used my structure in your explanation.  That's why I felt that the answer deserved a C.
I didn't use a text book... that came straight out of Turbo C++'s Help, but I guess you didn't read that too...