C Programming

UNIONS
This is a special data type which looks similar to a structure, but is very different. The declaration is,


	union  mixed {
		char  letter;
		float radian;
		int   number;
	};

	union mixed all;

The first declaration consists of a union of type mixed, which consists of a char, float, or int variable. NOTE that it can be ONLY ONE of the variable types, they cannot coexist.

This is due to the provision of a single memory address which is used to store the largest variable, unlike the arrangement used for structures.

Thus the variable all can only be a character, a float or an integer at any one time. The C language keeps track of what all actually is at any given moment, but does not provide a check to prevent the programmer accessing it incorrectly.


©Copyright B Brown. 1984-1999. All rights reserved.