Union vs Structure: What’s the Difference?

By | November 24, 2021
Union vs Structure

Union and Structure are the two most widely used data structures in C and C++. Both union and structure share the same syntax but they certainly have several differences, and that’s what gives rise to the union vs structure debate.

Though the syntax of the two data structures is quite similar, both tend to serve different purposes. We could say that every union data structure can be changed with structure, but every structure can’t be changed with the union.

Vamware

Nonetheless, this article will discuss all the key differences between union and structure that you should know if you want to work with them. So, without further ado, let’s get started!

Union vs Structure

To begin with the comparison, we will give a brief overview of these two data structures of the C programming language and learn their syntaxes with the examples.

What is Structure?

The structure is a user-defined data type available in C and C++ programming languages, which is used to collect heterogeneous data types with the same group name. Like an array, a structure can store many items, with a common group name. Also, the structure can store different items of different data types, which is exactly what makes it more efficient than an array.

Each element or item inside a structure has its individual memory location and size, that’s why the total size of a structure can be obtained by the sum of all its data types.  To define a structure, we use the struct keyword.

Structure Syntax:

struct structure_name {
       <data type> member
       <data type> member
                      } structure variable;

 Example:

struct student{
char name[10];
int roll_no;
char phone[10];
 };
int main()
{   student std;
  printf(" The Size of item structure = %d\n", sizeof (std));
  return 0;
}

#Output

The Size of item structure = 28

Here, the structure name is ‘student’, and name, roll_no, and phone are its members. Also, the std is a structure variable.

What is Union?

The union is a user-defined data type that can also store different data types and they all share the same memory location and size. The size of a union data type depends upon the largest data type stored in that union. Suppose if a union has 3 data types: a char, an int, and a float, then the memory allocated to the union, according to the float and the size of the union, will be equal to the size of the float.

As mentioned earlier, union and structure have the same syntax. However, to define a union data structure, we use the union keyword.

Union Syntax:

union union_name {

                                <data type> member

                                <data type> member

                                                } union variable;

Example:

union items{
           char i;
           int    j;
           float k;
           };

int main()

{
  union items ite;
  printf(" The Size of item Union = %d\n", sizeof (ite));
  return 0;
}

#Output

The Size of item Union = 4

Union vs Structure: Head to Head Comparison

Structure Union
We use the struct keyword to define a structure. To create a union, we use the union keyword.
For every variable in structure, the compiler allocates a different memory location. For every variable in the union, the compiler allocates the same memory location according to the largest variable.
In structure, if we change the value of one variable, it does not have any adverse effect on other variables of the structure. In union, if we change the value of one variable, it will have an adverse effect on other variables of the union.
Individual members can be accessed at a time. Only one member is accessible at a time.
We can store different values for different variables at the same time. In union, we can not store different values for different variables.
All the members can be initialized at once. All the members can’t be initialized at once.

Where to Use a Union and Structure?

Union: We use union where a single member is enough to serve the purpose. For example, we want a data structure that only accepts one value to find a student from a class. It’s up to you whether you want to enter the student roll no or his name to find him/her.

Structure: We use structure when we want all the members of it, and all the members must have distinct values. For example, let us suppose that we can make a structure for a student with a name and roll number, and we need to fill both the fields because every student has a name and a roll number.

Conclusion

Both union and structure have a significant role in the data structure. We use union where we are not sure if we want to use all the data members or just one of them is sufficient. On the other hand, we use a structure where we need all the data members with distinct values.

People are also reading:

Author: Paridhi Joshi

Paridhi Joshi is an expert web content manager with significant experience in content creation. Professionally she is dedicated to staying up to date with the latest trends and technologies in content writing and committed to bringing state-of-the-art web approaches to the workplace. She is an efficient multi-tasker who can complete multiple projects under strict deadlines.

Leave a Reply

Your email address will not be published. Required fields are marked *