Structure and Union are user defined data types, capable of holding data of various types.
The basic difference between structure and union is that structure provides a separate memory location for each member, whereas, Union provides a single memory location that is shared by all of its members.
Structure | Union |
---|---|
It can handle different elements of different datatypes at a time | It can handle only single element at a time of any datatypes |
Struct keyword is used to create a variable of a structure type | Union keyword is used to define variable of a union type |
Each element of structure gets memory spaces separately | All elements of union have to share the same memory space |
The value of an element doesn't get altered when changes are made to other elements | The value of an element does get altered when changes are made to elements |
The size of a structure variable is equivalent to or greater than the sum of its elements | The size of the union variable is always equivalent to element with the largest datatype |
In some situations, it can lead to memory wastage | In some situations, it makes a proper memory utilization as it locates memory to the maximum size of the element |
In C, Structures can be defined as a collection of variables of different data types that can be accessed by a single name. Therefore, these elements are together considered as a single entity.
The main advantage of the structure is that it makes easier to handle a large set of data.
In terms of memory allocation, a compiler provides memory to each element and the total size of the structure variable is, therefore, equal to or greater than the sum of all members.
It is possible the size can be more than the sum of its member as machine architecture can be varied.
Points to remember about Structure:
Syntax:
structure structure_name{ Variable, Variable2 }s;
#include <stdio.h> struct struct_name { int variable1; int variable2 }; int main( ) { struct struct_name str; str.variable1 = 10; printf( "Variable1 = %d \n",str.variable1); printf( "Memory size of structure: %d\n", sizeof(str)); return 0; }
Output:
Variable1 = 10
Memory size of structure : 8
Union is capable of holding only an individual element’s value at a time. A compiler allocates only a single storage area that can be used to store a value of a specific type.
All elements use the same memory area in order to store value regardless of its type.
Points to remember about Union:
Syntax:
union union_name{ Variable, Variable2 }u ;
#include <stdio.h> union union_name { int variable1; int variable2 }; int main( ) { union union_name un; un.variable1 = 10; printf( "Variable1 = %d \n",un.variable1); printf( "Memory size of union : %d\n", sizeof(un)); return 0; }
Output:
Variable1 = 10
Memory size of union : 4
C program to demonstrate the allocation of memory in case of Structure and Union
In case of Structure :
#include <stdio.h> struct struct_name { int variable1; int variable2}; int main () { struct struct_name str; str.variable1 = 10; str.variable2 = 20; printf ("%p\n", &str.variable1); printf ("%p\n", &str.variable2); return 0; }
Output:
0x7ffccf6e9260 0x7ffccf6e9264
In Case of Union:
#include <stdio.h> union union_name { int variable1; int variable2}; int main () { union union_name str; str.variable1 = 10; str.variable2 = 20; printf ("%p\n", &str.variable1); printf ("%p\n", &str.variable2); return 0; }
Output:
0x7fff08cacd44 0x7fff08cacd44
This post was last modified on July 4, 2022