• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

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;

    char Data[1];
    } SmallItem;

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


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.
  • 4
  • 2
1 Solution
Here is what the help file has to say...

  union (keyword)
A union is similar to a struct, except it allows you to define variables
that share storage space.

  union [<union type name>] {
    <type> <variable names> ;
  } [<union variables>] ;

For example,

  union int_or_long {
    int     i;
    long    l;
  } a_number;

Turbo C++ will allocate enough storage in a_number to accommodate the
largest element in the union.

Unlike a struct, the variables a_number.i and a_number.l occupy the same
location in memory. Thus, writing into one will overwrite the other.

Elements of a union are accessed in the same manner as a struct.

rstoneAuthor Commented:
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


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

do you understand how this works?

The Lifecycle Approach to Managing Security Policy

Managing application connectivity and security policies can be achieved more effectively when following a framework that automates repeatable processes and ensures that the right activities are performed in the right order.

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?
rstoneAuthor Commented:
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...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

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