A union is a user-defined data type that allows different members to share the same memory location. Like a structure, a union can contain members of different data types. However, all its members share the same storage, so it can hold a meaningful value in one member at a time.
Declaring, and Initializing Unions
Unions are defined and declared in the same fashion as structures however the keyword union is used instead of struct. The union is created only as big as necessary to hold its largest data member. A union can be initialised on its declaration but because only one member can be used at a time, only one member can be initialised. Unions initialised with a brace-enclosed initialiser only initialise the first member of the union but individual members can be initialised by using a designated assignment value(see initialising structures). The format and initialisation of the union statement are as follows:
#include <stdio.h>
int main( void )
{
union shared_tag {
char c;
int i;
float f;
double d;
} shared={33};
printf("%c character value",shared.c);
printf("\n%d integer value ",shared.i);
printf("\n%f float value",shared.f);
printf("\n%d double value",shared.d);
shared.i = 33;
printf("\n%c",shared.c);
printf("\n%i",shared.i);
printf("\n%f",shared.f);
printf("\n%f",shared.d);
shared.d = 33.333333333333;
printf("\n%c",shared.c);
printf("\n%i",shared.i);
printf("\n%f",shared.f);
printf("\n%f",shared.d);
return 0;
}
Output:
Integer value: 33
Double value: 33.333333
Character value: A
Explanation
- union shared_tag declares a union type with four members: c, i, f and d.
- shared = {.i = 33} initialises the i member to 33 using a designated initialiser.
- shared.i = 33 assigns a value to the integer member.
- shared.d = 33.333333333333 assigns a value to the double member. This overwrites the shared storage previously used by i.
- shared.c = ‘A’ assigns a character to the c member, using the same storage.
Individual union members are accessed using the dot (.) operator, just as structure members are.
Important: Although all union members share the same memory, reading a different member from the one most recently written does not perform a normal type conversion. The result may be implementation-dependent or otherwise unsuitable for the intended purpose. To convert a value, use an explicit type conversion instead.
For example, to convert a double value to an integer:
shared.d = 33.75;
int number = (int)shared.d;
printf("%d\n", number);
This prints 33 because converting a floating-point value to an integer discards its fractional part.
Unions and Pointers
Unions, like structures, can be accessed and manipulated using pointers. A pointer to a union is declared by placing an asterisk (*) before the pointer name. Individual union members can be accessed through a pointer using the arrow (->) operator.
#include <stdio.h>
#include <string.h>
int main(void)
{
union data
{
char c[10];
int i;
};
union data charvalue;
strcpy(charvalue.c, "union data");
union data *ptrcharvalue;
ptrcharvalue = &charvalue;
printf("%s", ptrcharvalue->c);
union data intvalue;
intvalue.i = 13;
union data *ptrintvalue;
ptrintvalue = &intvalue;
printf("\n%i", ptrintvalue->i);
return 0;
}
Output:
union data
13
This union can store either a character array (c) or an integer (i) at a time. Since all union members share the same memory location, assigning a value to one member overwrites the value previously stored in another member. Each pointer accesses the appropriate member using the -> operator.