Difference between Structure and Union

Key difference: A structure is defined by the struct statement, whereas a union is defined by the union statement. Both store data, but while the union allows storing different data types in the same memory location, a structure is primarily used to represent a record.

Memory Allocation In StructureIn C programming language, both structure and union are two different types of user defined data types, which means that they are two different ways that can be used to store data. Both structures and unions support only assignment = and sizeof operators; however, both are slightly different which makes them better suited for different tasks.

A structure is defined by the struct statement, whereas a union is defined by the union statement. Both store data, but while the union allows storing different data types in the same memory location, a structure is primarily used to represent a record.

Additionally, while a structure allows the combining of different data types, a union can be defined by many members, but each member can only contain a single value at any given time. The amount of memory required to store them also differs. A structure requires an amount of memory which is equivalent to the sum of the size of all the members. The amount of memory required to store a union, on the other hand, is always equal to that required by its largest member.

In a structure, each member has their own memory space, while in a union, a single block is used by all the members of the union. Furthermore, while altering one value in a structure will not affect the other members, the same cannot be said of a union. In a union, altering any one value of any member will affect the other member values.

Memory Allocation in UnionIn a structure, an individual member can be accessed at any time and all of these members can be initialize at once. Whereas in a union, only one member can be accessed at any given time and only the first member of a union can be initialized at a time.

It should be noted that structures and unions cannot appear as operands of the equality ==, inequality !=, or typecast operators; but both structure and union can be passed by value to functions and returned by value by functions.

Comparison between Structure and Union:

 

Structure

Union

Type

User-defined data type

User-defined data type

Language

C Programming Language

C Programming Language

Command

the struct statement

the union statement

Purpose

Used to represent a record.

Allows storing different data types in the same memory location.

Data items

Allows combining data items of different kinds.

Can define with many members, but only one member can contain a value at any given time.

Size

The amount of memory required to store a structure variable is the sum of the size of all the members.

The amount of memory required is always equal to that required by its largest member.

Memory Block

Each member has their own memory space

One block is used by all the members of the union.

Alteration

Altering one value of a member will not affect the other members of the structure

Altering any one value of any member will affect the other member values

Members

Individual member can be accessed at any time

Only one member can be accessed at any given time

Initialization

Several members of a structure can initialize at once.

Only the first member of a union can be initialized

Reference: Tutorials Point (Structures and Unions), ElectroFriends,
Geeks for Geeks, CS Fundamentals
Image Courtesy: thecrazyprogrammer.com

Most Searched in Environment Most Searched in Games and Recreation
Most Searched in Beauty and Style Most Searched in Home and Garden
Nova vs Chevelle
Kwashiorkor vs Marasmus
BMI vs Body Fat

Add new comment

Plain text