Unions are a special data type provided in C++ that allow for multiple data types to be stored in the same location.
Unions are defined outside main
the same way struct
are, with the only difference being that the keyword struct
is replaced by union
:
union Sample {char a;float b;int c;};
Once declared, instances of unions can then be created and used inside main
(or other functions) using the following syntax:
int main () {union Sample u_name;}
To declare an instance of a union, we must start with the keyword union
, followed by the name of the union we're using, which in this case is Sample
. Lastly, we need a name for the union instance we are creating, which in this case is u_name
.
Note: Giving a name to our instance is the same as initializing a variable with a name. Consider that we are creating a variable of type union
Sample
.
Once we have initialized u_name
, we can then use the .
operator to access the variables inside Sample
in the following manner:
int main() {union Sample u_name;u_name.a = 'c';u_name.b = 87.99;u_name.c = 103;}
So far, our implementation of union
seems no different from a struct
in C++. However, the important difference is that all the variables we initialized inside Sample
share the same memory space.
u_name.a
, u_name.b
, and u_name.c
would all share the same memory space. This means we could efficiently only use one variable at a given time.
In the example above, when we initialize u_name.a
to 'c'
, all the other variables will point to the same memory location and thus contain the equivalent of 'c'
as well.
Similarly, in the next line, when we assign u_name.b
the value 87.99
, we would in reality be updating all other variables as well.
Generally, unions are useful when you want to store data that could be one of multiple data types. A union could vary the data type of given data as required.
It would be considered a bad practice to use all variables for a given union
. Suppose we have some data that should be a char
. In the example above, we would utilize a
since it is of type char
. Once a
is assigned a value, b
and c
would essentially contain garbage values and should not be used unless the data type for our said data needs to be changed to either a float
or an int
.
#include <iostream>using namespace std;// creating unionunion Sample {char a;float b;int c;};int main() {// initialising a unionunion Sample u_name;// assigning value to au_name.a = 'c';// printing all valuescout << "a " << u_name.a << endl;cout << "b " << u_name.b << endl;cout << "c " << u_name.c << endl;cout << endl << endl;// assigning value to bu_name.b = 87.99;// printing all valuescout << "a " << u_name.a << endl;cout << "b " << u_name.b << endl;cout << "c " << u_name.c << endl;cout << endl << endl;// assigning value to cu_name.c = 103;// printing all valuescout << "a " << u_name.a << endl;cout << "b " << u_name.b << endl;cout << "c " << u_name.c << endl;cout << endl << endl;return 0;}
In the example above, we can see how once either a
, b
, or c
are assigned a value, the other two variables get updated and seem to contain an entirely random value. Essentially, the other two data types contain the same value implicitly converted into their own data type. That random value is essentially a garbage value and should not be used in most cases.
Free Resources