logo codesdope

Union


Unions are declared and used in the same way as structures. Like structures, it is also used to store different types of data.

Before going into the differences between structure and union, let's first see how to define a union.

Defining a Union

Let's look at the syntax of a union.

union union_name
{
data-type member-1;
data-type member-2;
data-type member-3;
data-type member-4;
};

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;
    std::string name;
    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 stored 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.

union student
{
    int roll_no;
    std::string name;
    int phone_number;
};
int main()
{
    struct student p1, p2, p3;
    return 0;
}

There is another method of declaring union variables where we declare these at the time of defining the union as follows.

union student
{
    int roll_no;
    std::string name;
    int phone_number;
}p1, p2, p3;

Like structure, we assign the name, roll no and phone number of the first student (suppose p1) by accessing its name, roll_no and phone number as follows.

p1.roll_no = 1;

p1.name = "Brown"

Till now, you must have found union similar to structure. Now let's look at the differences between the two.

Difference between union and structure


Before going into the differences, let's first look at an example.

#include <iostream>

using namespace std;

struct student1 {         // defining a struct
  	int roll_no;
  	char name[40];
  	int phone_number;
};

union student2 {          // defining a union
	int roll_no;
  	char name[40];
  	int phone_number;
};

int main()
{
	struct student1 s1;
	union student2 u1;
	cout << "size of structure : " << sizeof(s1) << endl;
	cout << "size of union : " << sizeof(u1) << endl;
	return 0;
}
Output
size of structure : 48
size of union : 40

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.

union in c

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 member's variables at the same time because each occupies a different memory space.

Now let's see an example of union.

#include <iostream>
#include <cstring>

using namespace std;

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

int main()
{
	union student p1;
	p1.roll_no = 1;
	p1.phone_number = 1234567822; 
	strcpy(p1.name,"Brown"); 
	cout << "roll_no : " << p1.roll_no << endl;
	cout << "phone_number : " << p1.phone_number << endl;
	cout << "name : " << p1.name << endl;
	return 0;
}
Output
roll_no : 2003792450
phone_number : 2003792450
name : Brown

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.

We 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 '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.

Let's see one more example.

#include <iostream>
#include <cstring>

using namespace std;

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

int main()
{
	union student p1;
	p1.roll_no = 1;
	cout << "roll_no : " << p1.roll_no << endl;
	strcpy(p1.name,"Brown"); 
	cout << "name : " << p1.name << endl;
	p1.phone_number = 1234567822;
	cout << "phone_number : " << p1.phone_number << endl;
	return 0;
}
Output
roll_no : 1
name : Brown
phone_number : 1234567822

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.

Share what you know
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.