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.

mon = 0
tue = 1
wed = 2
thu = 3
fri = 4
sat = 5
sun = 6

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.

mon = 1
tue = 2
wed = 3
thu = 4
fri = 5
sat = 6
sun = 7

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:

mon = 2
tue = 3
wed = 4
thu = 3
fri = 4
sat = 5
sun = 6

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.

Union

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:

struct shirt
{
char size;
int chest;
}mine;

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:

union shirt
{
char size;
int chest;
}mine;

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:

#include<iostream.h>
union shirt
{
char size;
int chest;
int height;
};
int main( )
{

shirt mine;
cout<<"\nSize of the union is : "<<sizeof(mine);
cout<<"\nWhat size (S/M/L)? ";
cin>>mine.size;
cout<<"\nThe size is : "<<mine.size;
cout<<"\nThe chest measurement is : "<<mine.chest;
cout<<"\nThe height measurement is : "<<mine.height;
cout<<"\n\nWhat is the chest measurement? ";
cin>>mine.chest;
cout<<"\nThe size is : "<<mine.size;
cout<<"\nThe chest measurement is : "<<mine.chest;
cout<<"\nThe height measurement is : "<<mine.height;
cout<<"\n\nWhat is the height measurement? ";
cin>>mine.height;
cout<<"\nThe size is : "<<mine.size;
cout<<"\nThe chest measurement is : "<<mine.chest;
cout<<"\nThe height measurement is : "<<mine.height;
return 0;
}

The output would be as below:

Size of the union is : 4
What size (S/M/L)? s
The size is : s
The chest measurement is : -858993549
The height measurement is : -858993549
What is the chest measurement? 23
The size is : 
The chest measurement is : 23
The height measurement is : 23
What is the height measurement? 12
The size is : -
The chest measurement is : 12
The height measurement is : 12

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:

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:

struct phonebook
{
int pin;
int tel;
};

void disp(struct phonebook p)
{
cout<<endl<<"The pincode is : "<<p.pin;
cout<<endl<<"The tel. no. is : "<<p.tel;
}

int main( )
{
phonebook m;
m.pin=60001;
m.tel=23651;
disp(m);
return 0;
}

The output is:

The pincode is : 60001

The tel. no. is : 23651

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.

Recap

• Scope of a variable refers to the region within which the variable exists.
• A local variable exists only within the block where it is declared whereas a global variable is visible throughout the entire program.
• Automatic variables will not retain their values whereas static variables will retain their value when program flow exits a function.
• An array is a collection of logically related variables of the same data type.
• Array elements occupy contiguous memory locations.
• Character arrays store strings and has to be terminated by the null character.
• A structure is a collection of logically related variables of different data types.
• Nesting of structures is permitted.
• Enumeration permits the programmer to assign different names to integer constants.
• A union is similar to a structure but allocates memory space sufficient to hold the maximum data type within the union. It doesn’t allocate space for all the data types.

Go back to the Contents Page