Union vs Structure

By | November 3, 2020
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 with minor differences, that’s what makes them comparable with others. Though the syntax is quite similar, both are used to serve a different purpose because of the memory allocation. We could say that for every Union data structure can be changed with Structure, but every Structure can’t be changed with the Union.so let’s discuss Union vs Structure.

Union vs Structure

Here in this article we will learn and compare these two data structures of the C programming language and learn all the difference between them.

Vamware

What is Structure?

A Structure is a user-defined data type present in C and C++, which is used to collect heterogeneous data types, with the same group name. Like an Array, Structure can store many items, with a common group name, but unlikely Structure can store a different kind of Data Types That 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 type.  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 structure name is ‘student’ and name, roll_no, and phone are its members where std is a structure variable.

What is Union in C?

The union is a user-defined data type which can also store different data type which all share the same memory location and size. The size of a union data type depends upon the largest data type store in that union structure. 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 that float.

As Union and Structure have the same syntax, to define a union data structure we use 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: Comparison

Structure Union
We use the struct keyword to define a Structure. To Create a Union, we use union keyword
For every variable in structure, the compiler allocates different memory location. for every variable in Union compiler allocate 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 then it will have an adverse effect on other variables of the Union.
Individual members can be accessed at a time Only one member can be accessed at a time
We can store different value for the different variable 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 with the help of any data member is enough to serve the purpose. For example, we want a data structure which only accepts one value that finds the student from a class and it’s on you whether you want to enter the student roll no or his name, here union will work efficiently.

Structure: We use Structure when we want all the members of it must have some value and they must be distinct. Suppose we can make a structure for a student with a name and roll number, and where we need to fill both the fields because every student has a name and a roll no.

Conclusion

Both Union and Structure have a significant role in the data structure. We use Union where we are not sure that we want to use all the data members or just one of them is able to do the work and we use a structure where we need all the data members with distinct functionality.

You May Also Interested In:

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 *