The UNION keyword.


The union keyword allows several variables of different type and size to occupy the same storage location.

The syntax to define a union is simular to the struct keyword as shown below:

	union union_def { int a; float b; char c;} ;

and a variable declared with either of these statements:

	union union_def union_var;
	union { int a; float b; char c;} union_var;

If you wish to initalise a variable you can say:

	union { int a; float b; char c;} union_var=97;

By default the first variable (a) is initalised.

To assign a value to a variable you can say:

	union_var.b=99.99;
	union_var.a=34;
	union_var.c='x';

It's important to note that the storage will only hold ONE value, looking at the three lines above, union_var.a overwrites union_var.b and then union_var.c overwrites union_var.a

I have yet to see more than one use for this keyword.


Examples:

Here is an example.


See also:


Top Master Index Keywords Functions


Martin Leslie