Difference between union and structure

Difference between union and structure

S.no
C Structure
C Union
1
Structure allocates storage space for all its members separately.

Union allocates one common storage space for all its members.

Union finds that which of its member needs high storage space over other members and allocates that much space
2
Structure occupies higher memory space.
Union occupies lower memory space over structure.
3
We can access all members of structure at a time.
We can access only one member of union at a time.
4

Structure example:

struct student
{
int mark;
char name[6];
double average;
};

Union example:

union student
{
int mark;
char name[6];
double average;
};
 5

For above structure, memory allocation will be like below.

int mark – 2B
char name[6] – 6B
double average – 8B
Total memory allocation = 2+6+8 = 16 Bytes

For above union, only 8 bytes of memory will be allocated since double data type will occupy maximum space of memory over other data types.

Total memory allocation = 8 Bytes



Though unions are similar to structure in so many ways, the difference between them is crucial to understand. 
This can be demonstrated by this example:

#include <stdio.h>
union job    //defining a union
{         
   char name[32];
   float salary;
   int worker_no;
}u;
struct job1 
{
   char name[32];
   float salary;
   int worker_no;
}s;
int main()
{
   printf("size of union = %d",sizeof(u));
   printf("\nsize of structure = %d", sizeof(s));
   return 0;
}
Output

size of union = 32
size of structure = 40

  • There is difference in memory allocation between union and structure as suggested in above example.
  •  The amount of memory required to store a structure variables is the sum of memory size of all members.
www.mskuthar.blogspot.com
Difference between union and structure

Memory allocation of structure variable in C programming

But, the memory required to store a union variable is the memory required for largest element of an union.

Memory allocation of union variable in C programming

What difference does it make between structure and union?

As you know, all members of structure can be accessed at any time. 
But, only one member of union can be accessed at a time in case of union and other members will contain garbage value.

#include <stdio.h>
union job 
{
   char name[32];
   float salary;
   int worker_no;
}u;
int main()
{
   printf("Enter name:\n");
   scanf("%s",&u.name);
   printf("Enter salary: \n");
   scanf("%f",&u.salary);
   printf("Displaying\nName :%s\n",u.name);
   printf("Salary: %.1f",u.salary);
   return 0;
}
Output

Enter name 
Hillary
Enter salary
1234.23
Displaying
Name: f%Bary   
Salary: 1234.2
Note: You may get different garbage value of name.

Why this output?

Initially,  Hillary will be stored in u.name and other members of union will contain garbage value. 
But when user enters value of salary, 1234.23 will be stored in u.salary and other members will contain garbage value. 
Thus in output, salary is printed accurately but, name displays some random string.

Passing Union To a Function

Union can be passed in similar manner as structures in C programming. 

Post a Comment

Previous Post Next Post