Defining a Union

Isn't this the same as the syntax of structure, except for the difference that in place of the keyword struct, we wrote the keyword union?

Let's take the same example we took in structure. Here we will take the name of union as 'student' and name, roll_no and phone_number as its members as follows.

union student
{
int roll_no;
char name[30];
int phone_number;
};

Declaration of Union Variable

We declare the variables of a union in the same way as we declare those of a structure.

Again, let's take the same example in which we want to store the roll no, name and phone number of three students. To do this, we will define a union named 'student' as we have defined above and then we will declare its three variables 'p1', 'p2' and 'p3' (which will represent the three students respectively) in the main function.

Talking about the above example, the amount of memory required to store a structure is the sum of the memory sizes of all its members. In the above example, the memory sizes of the variables roll_no and phone_number will be 4 bytes each (since both are of type integer) and the memory size of the character array name[40] will be 40 bytes (since the array occupies the memory of 40 characters and the size of char is 1). Thus, the memory occupied by the structure will be 4+40+4 = 48 bytes.

Now coming to the union, the memory size of a union is equal to the size of its member occupying the maximum space in the memory. The size of roll_no and phone_number is 4 bytes each and that of name[40] is 40 bytes. So, the union will occupy a memory space of 40 bytes.

We can access only one member of union at a time because we have only one location in memory for it, so only one of the member can be used at a time. All the other members will contain the garbage value (i.e. will get corrupted). This is not the case with structures where we can access all the members variables at the same time because diiferent memory spaces are occupied by each.

As you can see, the name got printed as it is and garbage value got printed as roll number and phone number. Let's see why this happened.

As you now know that we can access only one member of union at a time and the other members get corrupted. So, when we wrote p1.roll_no = 1, member 'roll_no' got accessed and got assigned a value a value '1'. After that, by writing p1.phone_number = 1234567822, we assigned a value to the member 'phone_number'. Finally, we assigned a string value "Brown" to the member 'name' as strcpy(p1.name,"Brown") and now, the other two members 'roll_no' and 'phone_number' contains a garbage value. Thus, the value of name got printed as it is and the values of the other two variables in the output are the garbage values of those two.

Here, first we assigned the value of roll_no as 1 and printed it. So currently, the value of the other members is some garbage value and that of roll_no is 1, so it got printed as it is. After that, we assigned a string value "Brown" to name, so now roll_no and phone_number contained garbage values and name got printed. Similarly, we printed phone_number.