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.
Difference between Union and Structure in C in Tabular Form
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 |
Structure
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:
- They are used to allocate sequentially
- Compiler allocates memory to every element of the structure separately
- The size of a structure variable is greater than or equal to the sum of its all elements
- The object of structure can handle all members at a time
Syntax:
structure structure_name{ Variable, Variable2 }s;
Code example of structure in C:
#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
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:
- A union can handle only a single member at a time
- The size of the union variable is always equivalent to the member with the largest datatype
- A compiler allocates the memory by considering the largest data element defined in the union
Syntax:
union union_name{ Variable, Variable2 }u ;
Code example of Union in C :
#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