...

/

Anonymous Unions and Dissecting Other Members

Anonymous Unions and Dissecting Other Members

You will learn about anonymous unions and see how unions can be used for accessing individual bytes of variables of other types in this lesson.

We'll cover the following...

Anonymous unions

Anonymous unions specify what members of a user-defined type share the same area:

Press + to interact
import std.stdio;
struct S {
int first;
union {
int second;
int third;
}
}
void main () {
writeln(S.sizeof);
}

The last two members of S share the same area. So, the size of the struct is a total of two ints: 4 bytes needed for first and another 4 bytes to be shared by second and third.

Dissecting other members

Unions can be used for accessing individual bytes of variables of other types. For example, they make it easy to ...