Structure vs. Union

Structure:

The structure is a group of variables of different data types represented by a single name. It is a user-defined data type present in the C language. In other words, the structure creates a data type that can be further used to group the items of different types into a single type.

Array allows you to define types of variables that can hold many data items of the same kind. In the same way, the structure will enable you to combine data items of different sorts. Structures are used to represent a well-defined record.

Declaration of structure:

‘Struct’ keyword is used to define a structure. It defines a new data type with one or more members.

Different ways of assigning values to structure members:

There are three ways to assign values:

  1. Dot (.) operator –
  • All members assigned in one statement –
  • Designated initializers.

Consider a record to which you need to store the data of several students like name, ID, address, phone numbers, etc. Since multiple student’s data needs to be stored, it is best to select a structure as it overcomes the problem of creating a different variable for each attribute. You can create variables for the structure with name, ID, address, phone numbers as a member of it.

Eg:

Union:

Union is also a user-defined type similar to ‘structure’ in C programming language except for one difference; structure allocates enough space to store all their members, while union can hold only one member value at a time.

It is a special data type present in the C programming language, allowing you to store data type in the same memory location. You can define a union with many members, but only one member can have a value at any given point in time. It provides an efficient way to utilize memory location for multiple purposes.

Declaring a union:

When a union is defined, it creates a user-defined type variable. Although, no memory is allocated. Variables should be made to allocate memory for a union, and further operations can be performed.

Union tag in the declaration is optional and each member definition is a normal definition of the variable, for example, int i, float f, and any other valid definition of the variable. At the end of the union declaration, before semicolon, you can either specify one or more variables to it or it is completely optional.

Accessing members of a union:

  1. The dot operator (.) – is used to access members of the union.

E.g.: b1.price is used to access the price of book 1

  • Arrow (->) – is used to access pointer variables.

E.g.: (*b3).price or b3->price is used to access the price of book three, which is a pointer.

Memory occupied by a union keyword is large enough to contain the largest member of the union. For example, let us consider a data type which occupies 20 bytes of memory space because it will be the maximum space which can be occupied by a character string.

Differences between structure and union:

StructureUnion
Separate memory allocation is followed while using struct keyword.Same memory location will be allocated to all the members.
struct structure_name {     data_type var1;     data_type var2;     … }variable1, variable2;union union_name {     data_type var1;     data_type var2;     … } variable1, variable2;
Keyword used is struct.Keyword used is union.
Single way of viewing.Multiple way of viewing.
Size of the structure is equal to the sum of size of all the data.Size of union is equal to size of the largest member.
Stores distinct value for all members.Stores same value for all the members.

Pin It on Pinterest

Share This