Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Could someone explain "union" to me?

Posted on 1999-07-27
7
Medium Priority
?
275 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:rstone
[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
  • 4
  • 2
7 Comments
 
LVL 10

Accepted Solution

by:
viktornet earned 200 total points
ID: 1263984
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.

 Syntax:
  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.
==========

..-=ViKtOr=-..
0
 

Author Comment

by:rstone
ID: 1263985
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?

0
 
LVL 10

Expert Comment

by:viktornet
ID: 1263986
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=-..
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 3

Expert Comment

by:marcjb
ID: 1263987
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.  */

0
 
LVL 10

Expert Comment

by:viktornet
ID: 1263988
Why on earth did you give me a C?
0
 

Author Comment

by:rstone
ID: 1263989
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.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1263990
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...
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

660 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