Unions and more

The following topics are covered in this section:

Enumerated Types

To enumerate means to count. In C++ an enumerated data type permits you to assign different names to your integer values.

The syntax is:

enum enumeration-name{the various constants separated by a comma};

For example:

enum day{mon,tue,wed,thu,fri,sat,sun};

will consider ‘mon’ as being equivalent to 0, ‘tue’ as 1, ‘wed’ as 2 and so on.

If you want to start from zero onwards then you have to specify it:

enum day{mon=1,tue,wed,thu,fri,sat,sun};

Now mon will be equal to the integer value of 1, tue will be 2 and so on.

To create a variable of the enumerated type you simply need to write:

day today, tomm;

Or you could combine the enumeration definition and variable declaration into one statement as follows:

enum day{mon,tue,wed,thu,fri,sat,sun} today, tomm;

Whichever method you use the result is the same. ‘today’ and ‘tomm’ are variables of type ‘day’ which can take any one of the constant values (sun,mon,tue etc…). The integer values that you assign could also be negative integers. Is it possible for 2 enumeration constants to have the same value?

Check this out:

enum day{mon=2,tue,wed,thu=3,fri,sat,sun};

The values would be as below:

It is very much possible to make two or more enumeration constants have the same integer value.

You might wonder of what use is the enumeration type? Actually whatever you do with enumerated types is actually equal to working with integers. You can compare enumerated variables, you can increment them etc. The only advantage is that instead of using numbers you can use some useful names. This is especially useful in larger programs where you might want to indicate some particular state by using a name rather than a number. This would aid in understanding the program without any confusion. You could even consider the example that we’ve considered above; would it be better to refer to days of the week as ‘sun’, ‘mon’ etc…or would it be better to have integer values of 1,2,3 etc.?

Remember: When you attempt to display any enumeration constant, the program will display only the corresponding integer value.


Unions are similar to structures in certain aspects. They also group together variables of different data types and individual members can be accessed using the dot operator. The difference is in the allocation of memory space. A structure will allocate the total space required for a structure variable but a union will allocate only the space required by one element (the element that occupies the maximum size). Suppose you have a union consisting of 4 variables (of different data types), then the union will allocate space only to the variable that requires the maximum memory space. For example:

will allocate a memory space of 5 bytes to the structure variable ‘mine’ (assuming 4 bytes for an integer and 1 byte for a character).

The same thing could be re-written using unions as follows:

Now, the union variable ‘mine’ will be allocated only 4 bytes (the compiler knows that a character will require one byte while an integer needs 4 bytes. Hence it allots ‘mine’ a total of only 4 bytes).

Can you reason out what are the consequences of conserving memory space in this fashion? It means that when you use a union only one element will have a valid value at any instance. In the above example, we can have a valid value for either mine.size or for mine.chest

Both the elements cannot have a valid value at the same time. You might be wondering what is meant by a valid value; just check out the example below:

union shirt
    char size;
    int chest;
    int height;
int main( )


The output would be as below:

Explanation: You should be able to understand what is meant by a valid value. When we enter the value for ‘chest’, the value of ‘size’ is a weird symbol (an invalid value).

The memory allocation would be as shown below:

Text Box: Both integers (height and chest) take up same memory space.
Text Box: Character stored here
Text Box: Text Box: Text Box: Text Box: Text Box: The compiler realizes that the maximum space required is 4 bytes and allocates only 4 bytes for the union variable ‘mine’.
Text Box: Integer height (4 bytes)
Text Box: Integer chest (4 bytes)
Text Box: Character size (1 byte)
Text Box: Text Box: Text Box: Text Box: Text Box: Text Box: Text Box: Text Box: Text Box:














The above diagram should make it clear as to why ‘height’ and ‘chest’ values are always the same. Both are integers, both take up 4 bytes, both occupy the same area, so when the compiler reads the 4 byte integer it will display the same value in both cases. It is up to the programmer to ensure that he operates on the correct data. Again, the reason for the invalid character display should be clear: Once the integer value (for height or chest) is stored and you attempt to read the character value the compiler will read one byte out of the four bytes (which are used for the integer).

The main use of unions is when you want to conserve memory space and when you are sure that at any instance you will not require the values for all the elements of a variable. For instance let us say that we have a stockpile of T-shirts in a factory. The computer could maintain a database about the sizes of each T-shirt. The size of T-shirts is either mentioned as small, large and medium or the size is specified in terms of the chest size. Thus every T-shirt will have only one of the two specifications (either a letter or a number). A union is ideal for this purpose.

Passing Structures to a Function

If you want to pass an individual element of a structure to an element, you can simply use the dot operator and pass the element. If you want to pass an entire structure to a function, then simply pass the structure variable as an argument. Let’s see an example:

The output is:

There isn’t much of a problem in passing a structure to a function but this method has a significant drawback when used with large structures. You will realize this when we discuss in depth about passing values to functions.


Go back to the Contents Page

Copyright © 2004 Sethu Subramanian All rights reserved.