Structure vs Union in C – Difference and Comparison

What is Structure in C?

A structure is a value type that can include functionality and data. A structure is a composite data type that unites variables in a memory block of potentially diverse types under one name. The keyword struct can be used to declare a structure, and it is possible to arrange objects of various types together using a structure.

In C programming, a user-defined datatype called structure enables a programmer to aggregate data of various types. It is possible to arrange objects of various types together using a structure. When objects of potentially diverse types are grouped into one type, a structure provides a data type that can be utilized for this.

Furthermore, multiple data types for the same object are stored together in structures. It is useful when maintaining information on a single item of several or related data types, such as name, address, phone, Etc.

Because it represents complete records using a single name, maintaining the entire record is simple. The structure enables a single argument to pass an entire set of records to any function. A collection of structures can also be made to hold a variety of comparable data.

What is Union in C?

In the C programming language, a union is a user-defined data type that enables the storage of many data types in the same memory address. It is a composite data type that unites variables of potentially diverse kinds under one name in a memory block, making it possible to retrieve the various variables using a single pointer.

It is possible to combine things of different types using a union. By combining elements of potentially diverse types into one type, a union provides a data type that can be utilized for this purpose. Moreover, compared to the structure, the union uses less memory, and it is only the most prominent data member can be accessed directly when using a union.

When it is desirable to use less (or the same) memory for several data members, this technique is used. All its data members are given memory space proportionate to the size of the most prominent data member.

A user-defined type is created when a union is defined. However, no recollection is set aside. It needs to construct variables to allocate memory for a specific union type and work with it.

Difference Between Structure and Union in C

Structures and unions are data types in the C programming language and are both used to store multiple values in a single location.

The main difference between structures and unions is that structures store each member in a different memory location, while unions store all members in the exact memory location.

With one important exception, a union is a user-defined type similar to structs in C. In contrast to unions, which can only contain one member value at a time, structures allow adequate room to store all of their members.

Comparison Between Structure and Union in C

Parameter of ComparisonStructure Union   
Usage of keywordsThe phrase of structure may be used by a user to define a Structure.The keywords are used to for a Union.
Implementation InternallyStructure in C is implemented internallyReceives memory allocation
Connecting the Members  Has capability to accessOne member can only be accessed at once by a user.
ConfigurationA user can initialize several members of a Structure simultaneously.  A user can only initiate the first member of a Union at a time.
Value ShiftingNo affect even there is a change in member’s valueOther members are altered when there is a change in one member’s value

References

  1. http://squoze.net/UNIX/v7/files/doc/15_lint.pdf
  2. https://dl.acm.org/doi/abs/10.1145/1134650.1134659