Difference between Structure and Union
STRUCTURES UNION
Struct keyword is used to declare the Union keyword is used to declare
structure the Union
Structure variable will allocate memory Union variable will allocate common
for all the structure members separately. memory for all the union members.
Example:
Example:
union Employee{
struct Employee{
int age;
int age;
char name[50];
char name[50];
float salary;
float salary;
};
};
It allows us to access any or all the
members at any time. It allows us to access only one
union member at a time.
Members do not share memory in a Members share the memory space in a
structure. union.
Size of the structure is equal to the sum of
size of the each member Size of the union is equal to the size of
the largest member.