What is a union in D Language?
Union
A union is a data type that can be considered special in D. It gives us the ability to have different data types in the same memory or storage location. The defined members of a union can be several, but only one of these members can have a value at any given time. Unions efficiently utilize a single memory location due to its multi-purpose ability.
General syntax for union definition
The union statement creates a new user-defined data type, with more than one member base type for our program. Below is the general syntax used to define a union:
union union_name{
define member
}[one_or_more_union_variables]
union: The keyword used to define a union.union_name: An optional tag name given to a union. It is any legal D identifier.define member: The part of the union statement block. We must declare more than one union member of any basic D inbuilt data type or a user-defined one.one_or_more_union_variables: A single or multiple variable we want to be of the union data type we just defined. It is optional as well.
Example
union example {
int my_int;
float my_float;
char my_str[40];
} sample_variable;
It implies that a variable of data type example can store an integer, a floating-point number, or a string of characters. It means one variable (a single memory location) can be used to store several types of data. Depending on our requirement, any built-in or user-defined data types can be used inside a union.
Accessing a Union
The sample_variable variable can be of any member type that is defined above just by doing this inside the main method. That will allow the sample_variable to hold an integer as implied by the my_int member.
example sample_variable;
sample_variable.my_int =20;
Code
Let’s look at the code below:
import std.stdio;//define a unionunion Example {int my_int;float my_float;char[40] my_str;};void main( ) {// declare variable sample_var as type exampleExample sample_var;//assign value to the variablessample_var.my_int = 10;sample_var.my_float = 220.5;sample_var.my_str = "I am loving this";//check size of the variableswriteln( "size of : ", sample_var.sizeof);writeln( "data.my_int : ", sample_var.my_int);writeln( "data.my_float: ", sample_var.my_float);writeln( "data.my_str : ", sample_var.my_str);}
Explanation
- Lines 3 to 7: We have the definition of a union
Example, with allowable data types ofint,float, andstring. - Lines 9 to 21: We have the
mainfunction blocks, which return no value. - Lines 11: We declare a variable
sample_varas the type ofExampleinside functionmain. - Lines 13 to 15: We give
sample_varseveral values based on the basic types in the union. - Lines 17 to 20: We have some print statements to print these defined variables to screen.
Output
The value of sample_var.my_int and sample_var.my_float are output erroneously. This happened because in the case of several assignments to a particular union variable, in this case, sample_var, the variable with the largest value, sample_var.my_str is automatically displayed correctly while the rest are wrong.
Note: Unions have a size that is the size of the largest value of the union.