Unions

A union is a user-defined data type similar to a structure in that it can have any number of members, each with its own data type. The difference between a union and a structure is that all of the members of a union occupy the same location in memory. A union can therefore only contain one object from its list of members at any given time (although the object can be a complex data type such as an array or structure). A union is declared using the following format:

union union_name
{
  type member_name_1;
  type member_name_2;
  type member_name_3;
  . . .
} my_unions; // optional declaration of union variables

In the above example, union_name is the name given to the union type, while my_unions represents an optional list of objects of type union_name. The member elements of the union and their data types are listed between the curly braces { }. The amount of memory allocated to the union will be sufficient to store the data type of the largest member of the union. Consider the following declaration:

union myUnion
{
  char category;
  int count;
  double price;
} myUnionVar;

In this example, the member with the largest data type is price, which is a double-precision floating point number, so the union occupies eight bytes (the size of the data type double) in memory.

In order to work with a union member, you must identify both the union object and the member element with which you wish to work. The following program statements demonstrate two alternative uses of the myUnionVar variable created when we declared the myUnion data type above:

myUnionVar.count = 27;   // accessed as type int
myUnionVar.price = 2.49; // accessed as type double

Note that in each case, the member element is referenced using the object's name, and the name of the member element, separated by a period. The following short program demonstrates the use of unions (note that you can declare and initialise a union at the same time):

// Example Program 1

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;

union myUnion
{
  int var1;
  double var2;
};

int main()
{
  string input_str;
  union myUnion myNumbers = { 10 }; // var1 = 10
  cout << "myNumbers.var1 evaluates to: " << myNumbers.var1 << "\n";
  cout << "\nPress ENTER to continue.";
  getline( cin, input_str );
  myNumbers.var2 = 3.141592653589;
  cout << "\nmyNumbers.var2 evaluates to: " << myNumbers.var2 << "\n";
  cout << "\nPress ENTER to continue.";
  getline( cin, input_str );
  return 0;
}


The output from example program 1

The output from example program 1



Anonymous unions

Anonymous unions are declared in the same way as other unions, but without either a name or a declarator list of objects of this type. An anonymous union is declared using the following format:

union
{
  type member_name_1;
  type member_name_2;
  type member_name_3;
  . . .
};

The member elements of the union can be accessed directly, in the same way as non-member variables, using their member names alone. This is demonstrated by the following short program:

// Example Program 2

#include <string.h>
#include <iostream>
using namespace std;

int main()
{
  string input_str;
  union
  {
    int var1;
    char *var2;
  };

  var1 = 25;
  cout << "Var1 is " << var1 << "\n\n";
  var2 = "part of the union!";
  cout << "Var2 is " << var2 << "\n\n";

  cout << "\nPress ENTER to continue.";
  getline( cin, input_str );
  return 0;
}


The output from example program 2

The output from example program 2

Bear in mind that the member names used within an anonymous union must not conflict with other names declared with the same scope within the program.