Difference Between Structure And Union In C programming With Examples

What Is Structure In C?

Structure is a group of variables of different data types represented by a single name. Similar to array structures are used to represent a collection of data items but of similar and different types using the single name. Structure is a user-defined datatype where we have to design and declare a data structure before the variable of that type are declared and used. So it can be said that structures help to organize complex data in a meaningful way.

The main point of distinction between structures and unions in C is that structure possesses a separate memory location that is allocated to each member. In contrast, the members forming a union possess the same memory location.

What You Need To Know About Structure

  • Members of structure do not share memory. So a structure need separate memory space for all its members i.e all the members have unique storage.
  • To define structure, ‘’struct’’ keyword is used.
  • Total memory size required is equal to the sum of the size of memory of all the members in the structure.
  • All the members can be accessed at the same time.
  • All members of the structure can be initialized.
  • An unknown structure cannot be declared.
  • A single way is being provided for viewing each memory location.
  • Alteration in value of any member does not in any way affect the value of other members.
  • Used when programmers need to store distinct values for all the members in distinct memory.
  • Structure stores different values for its members.

What Is Union In C?

A union is a special data type available in C that allows to store different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple-purpose.  In general, C programmers use a union for storing values belonging to several data types.

Unions are conceptually similar to structures. The syntax to declare/define a union is also similar to that of a structure. The only difference is in terms of storage. In structure, each member has its own storage location, whereas all members of union uses a single shared memory location which is equal to the size of its largest data member.

The keyword ‘’union’’ defines union, and its declaration is quite similar to that of a structure. Here, the variable is capable of storing values of different data types-be it an integer, string or float.

What You Need To know About Union

  • A union shares the memory space among its members so no need to allocate memory to all the members. Shared memory space is allocated i.e equivalent to the size of member having largest memory.
  • To define union, “union’’ keyword is used.
  • Total memory size required is equal to the memory size required by the largest data member in the union.
  • One member whose value recently stored in the memory can be accessed at a time.
  • Only the first member of union can be initialized.
  • An anonymous union can be declared.
  • Offers multiple ways of viewing the same memory location.
  • Change in value of one member affects the value of other members.
  • Used when type conversions are needed.
  • Union stores the same value as attributed to all members.

Difference Between Structure And Union In Tabular Form

BASIS OF COMPARISON STRUCTURE UNION
Memory Sharing  Members of structure do not share memory. So a structure need separate memory space for all its members i.e all the members have unique storage.   A union shares the memory space among its members so no need to allocate memory to all the members. Shared memory space is allocated i.e equivalent to the size of member having largest memory.  
Keyword To define structure, ‘’struct’’ keyword is used.   To define union, “union’’ keyword is used.  
Total Memory Size Total memory size required is equal to the sum of the size of memory of all the members in the structure.   Total memory size required is equal to the memory size required by the largest data member in the union.  
Access Of Members All the members can be accessed at the same time.   One member whose value recently stored in the memory can be accessed at a time.  
Initialization All members of the structure can be initialized.   Only the first member of union can be initialized.  
Declaration An unknown structure cannot be declared.   An anonymous union can be declared.  
Viewing Of Memory Location A single way is being provided for viewing each memory location.   Offers multiple ways of viewing the same memory location.  
Alteration In Value Of Members Alteration in value of any member does not in any way affect the value of other members.   Change in value of one member affects the value of other members.  
Use Used when programmers need to store distinct values for all the members in distinct memory.   Used when type conversions are needed.  
Storage Of Value Structure stores different values for its members.   Union stores the same value as attributed to all members.  

Similarities Between Structure And Union

  • Both structure and union have the same way of declaring itself, creating variables and the same way of accessing members of the variables.
  • The members of a union and structure can be in the form of objects of any data type, including arrays or other unions and structures. It is also possible for a member to comprise of a bit field.
  • Both structure and union can be passed to a function by both the methods call by value and call by reference.
  • Both structure and union has the same way of accessing data member using member variable followed by dot (.) operator.
  • Both structure and union can be passed by value to functions and returned by value by functions.
  • Both structures and unions support only assignment= and sizeof operators.

Comments are closed.