Online Tutorials & Training Materials | STechies.com
Register Login

Difference Between Structure and Union in C

|| || 0

Difference Between Structure and Union in C
Stechies

C provides five different ways of creating custom data. These are bit-field’, ‘union’, ‘structure’, ‘, ‘enumeration’ and ‘typedef’. Union and structure in C are container data types designed to hold any kind of data. An important 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. This article aims to throw more light on the main differences between structure and union in C. Read on to know more about structure vs union.

What is Structure?

Structure in C pertains to user-defined container data type that helps users combine data types comprising of different kinds to form a single type in any given block of memory. It is capable of containing both complex and simple data types that are related to each other, which would fail to make sense otherwise. All members present in the structure are provided with their individual memory locations that can be retrieved and accessed readily and at any time.

C programmers opt for a structure when large quantities of data require grouping as in a member directory used for the storage of name, address, numbers, etc. of different members belonging to a club or association. The information pertaining to each member is usually depicted in ascending order with each memory location starting at different offset values. Any alteration in the value of a specific member will not have any impact on the values provided to other members in a structure.

The ‘struct’ keyword is utilized for defining a structure pertaining to different data types falling under a single name. For instance, an employee database created in C with structure stores age, phone, salary details, employee’s name, etc. under a single entity. The ‘struct’ keyword informs the compiler that a specific structure is declared.

It is possible to access the members of a structure via two types of operators:

a. Structure pointer operator

b. Member operator

What is a Union in C?

Unions in C are in the form of a special data type that allows for the storage of different kinds of data types in a single memory location. Though it is possible to define a union containing several members, only a singular member can be provided with a value at any given point of time. In general, unions in C work to use any given memory location for different purposes. A union is considered to be quite similar to a structure in C. Union in C is defined by using the keyword ‘union’. In this case, the members overlay the memory allocated to each other. Usually, unions are large enough to fit in all its members.

When the union becomes associated with a variable, the compiler considers the size of the largest available memory and allocates the same to it. In other words, the size of the largest data member defines the size of a union and is equal to it. As the address of all members in a union is the same, it effectively means that each member would begin at the same offset value. Any alteration in the value of a specific member will impact the values provided to other members in a union. In general, C programmers use union for storing values belonging to several data types.

The definition of what is union in c is incomplete without knowing that a union is defined by the keyword ’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 integer, string or float.

Key Difference between Union and Structure

The main differences between structure and union in C can be best understood under the following heads:

1. Keyword

A structure is defined by using the keyword ‘struct’ while ‘union’ is the keyword used for defining a union.

2. Memory Allocation

Within a specific structure, separate memory space is allocated for each member. Each member occupies a different memory location and does not share any memory. On the other hand, a union delegates the same memory space for all members and the allocation of shared memory location becomes a possibility.

3. Member Access

Only one member in a union is accessible at any given point of time even though the union stores a singular value for all its members. Conversely, as multiple values are capable of being stored in a structure, a member can be retrieved and accessed at any given point of time.

4. Size

The union’s size is equal to the size of its largest member while the size of a structure is equivalent to the sum of the sizes of all members, or more.

5. Initialization

In case of a union, the first member is the only member that is initialized with the value provided to its data type. Conversely, several members are capable of being initialized in a single go in case of a structure.

6. Value

A union stores the same value as provided to all its members; the change in the value of a single member impacts the value of all others. On the other hand, a structure stores different values provided to all its members; any change in value of a member will not impact the value of others.

Structure vs. Union

Now that you are aware of the main structure and union difference, here’s a table to facilitate the cause further:

Point of Difference

Union

Structure

Keyword

‘union’ is used to define a union

‘struct’ is used to define a structure

Shared memory space

Members share memory space

No memory space is shared by members

Retrieval and Accessibility of members

Only a single member can be retrieved and accessed at a time

Any member is capable of being retrieved any time

Initialization

Only the first member is capable of being initialized

Several members can be initialized at any given time

Size

Size of a union is equal to that of the largest member

The size of a structure is equal to the sum total of the sizes of all members                                                              

Alteration in value

Change in value of one will affect the value of other members

Alteration in value of any member fails to impact the value of others

Values

Stores the same value as attributed to all members

Stores different values for its members.

Way of Viewing

Offers multiple ways of viewing the same memory location

A single way is offered for viewing each memory location

Anonymous declaration

An anonymous union can be declared

An anonymous structure cannot be declared

Similarities between Structure and Union in C

a. Both structures and unions are user-defined data types that are useful for storing data of various types in the form of a single unit.

b. 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.

c. Assignment = and sizeof operators is supported by other structures and unions. The structures/ unions in the assignment should comprise of the same member types and members.

d. A union or structure can be returned by ‘value by functions’ and passed by ‘value to functions’. Here, the argument has to be of the same type as that provided to the function parameter. A union or structure is to be passed by value, in a manner similar to scalar variables, and as a given corresponding parameter.

e. The ‘.’ operator is put to use for accessing members in both cases.

Conclusion

Both, structure and union are container datatypes that contain members belonging to different types. The use of structure and union in c programming is dependent on the purpose of their application. programmers use structures for the sake of storing distinct value for its members in a single memory location. On the other hand, a union in c program comes in handy when type conversion is required. Choose between a union and structure accordingly. We hope that you have gained a fair understanding of structure vs union in c in this article. It will help to go through the application in structures and unions in c examples to improve upon your understanding further.


Related Articles

0.0119 seconds.