Why unions?
You want to store records of 20 students.
Each record includes student’s rollno,name,percentage,grade,division.
Now,primary division student have measure of talent as grade and secondary students have percentage.
Suppose we use a structure for student then any student will have all 4 attributes at a time.
But for a primary division student we don’t want to store its percentage but only its grade and vice versa for a secondary division student.
So as said previously suppose there 20 students,then we will create an array of structures for them.As we only need grade for a primary division student and percentage for secondary division student,we are wasting memory.
Suppose out of 20,primary division students=15,secondary division students=5.
Consider percentage to be in float and grade is a char.Let a float take 4 bytes and char 1 byte.Then we are wasting
4*15 + 1*5=65 bytes
What is a union?
Datatype that enables you to store different data types in the same memory location.
- Representation:
union [union tag]
{
member definitions; //datatype var_name (char grade)
}one or more union variables;
union [union tag]
{
member definitions; //datatype var_name (char grade)
}one or more union variables;
[union tag] is optional if you are straightaway declaring variables after union definition i.e just before the ;
This is OK.
union talent_measure
{
char grade;
float percentage;
}tm;
int main(void)
{
printf("%d",sizeof(tm));
return 0;
}
But if you don’t want to declare the union variables just after the union definition,then you have to use the tag.The below is not OK.
union
{
char grade;
float percentage;
};
int main(void)
{
union tm;
printf("%d",sizeof(tm));
return 0;
}
The below is OK.
union talent_measure
{
char grade;
float percentage;
};
int main(void)
{
union talent_measure tm;
printf("%d",sizeof(tm));
return 0;
}
Using typedef
If you don’t want to specify union keyword while declaring an union variable in a function and you just want to use the tag,we use typedef keyword.
In the above code snippet,rather than writting
union talent_measure tm;
We can write
talent_measure tm;
if we specify typedef keyword while defining the union i.e
typedef union
{
char grade;
float percentage;
}talent_measure;
To access union members
1. Using Dot operator via union variable
union_variable.member_var;
tm.grade='A';
for a primary student or
tm.percentage=90.0f;
for a secondary student
2. Using arrow operator via pointer to a union variable
Datatype of Pointer to union variable would be union_name*
A member variable of union can be accessed via pointer as given below
typedef union
{
char grade;
float percentage;
}talent_measure;
int main()
{
talent_measure tm,*ptr_tm;
ptr_tm=&tm;
tm.grade='A';
printf("%c %c",ptr_tm->grade,(*ptr_tm).grade));
return 0;
}
Space allocated to union variables?
Same as size of largest datatype declared in union.
For eg,our union talent_measure has variables of 2 datatypes.Size of larger datatype amongst the two is float.So space of 4 bytes is allocated to union variable tm.
Example 1
union u
{
int a,b;
}v;
int main()
{
v.a=40;
printf("%d %d",v.a,v.b);
v.b=50;
printf("\n%d %d",v.a,v.b);
return 0;
}
O/p =
40 40
50 50
Unions can be nested inside structures or inside unions.
struct student
{
int roll_no;
char name[15];
union talent_measure tm; /*if typedef is not used for union,if it is used
no need to specify union keyword */
}st[20];
No comments:
Post a Comment