You must be wondering, why do we need unions? Generally, unions can be very useful when declared inside a structure. Consider an example in which you want a field of a structure to contain a string or an integer, depending on what the user specifies.
UNIONS
INSIDE STRUCTURES
You
must be wondering, why do we need unions? Generally, unions can be very useful
when declared inside a structure. Consider an example in which you want a field
of a structure to contain a string or an integer, depending on what the user
specifies. The following code illustrates such a scenario.
#include <stdio.h>
struct student
{
union
{
char name [20];
int roll_no;
};
int marks;
};
int main()
{
struct student stud;
char choice;
printf("\n You can enter the
name or roll number of the student");
printf("\n Do you want to
enter the name (Y or N): ");
gets (choice);
if (choice=='y' || choice=='Y')
{
printf("\n Enter the name:
");
gets (stud.name);
}
else
{
printf("\n Enter the roll
number: ");
scanf("%d",
&stud.roll_no);
}
printf("\n Enter the marks:
");
scanf("%d",
&stud.marks);
if (choice=='y' || choice=='Y')
printf("\n Name: %s",
stud.name);
else
printf("\n Roll Number: %d
", stud. roll_no);
printf("\n Marks: %d",
stud.marks);
return 0;
}
Now
in this code, we have a union embedded within a structure. We know, the fields
of a union will share memory, so in the main program we ask the user which data
he/she would like to store and depending on his/her choice the appropriate
field is used.
Note
Pointing
to unions, passing unions to functions, and passing pointers to unions to
functions are all done in the same way as that of structures.
Programming in C: Unit IV: Structures and Union : Tag: : with Example C Program - Unions Inside Structures
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation
Professional English II
HS3251 2nd Semester 2021 Regulation | 2nd Semester Common to all Dept 2021 Regulation
Statistics and Numerical Methods
MA3251 2nd Semester 2021 Regulation M2 Engineering Mathematics 2 | 2nd Semester Common to all Dept 2021 Regulation
Engineering Graphics
GE3251 eg 2nd semester | 2021 Regulation | 2nd Semester Common to all Dept 2021 Regulation
Physics for Electrical Engineering
PH3202 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation
Basic Civil and Mechanical Engineering
BE3255 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation
Electric Circuit Analysis
EE3251 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation
Physics for Electronics Engineering
PH3254 - Physics II - 2nd Semester - ECE Department - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation
Electrical and Instrumentation Engineering
BE3254 - 2nd Semester - ECE Dept - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation
Circuit Analysis
EC3251 - 2nd Semester - ECE Dept - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation
Materials Science
PH3251 2nd semester Mechanical Dept | 2021 Regulation | 2nd Semester Mechanical Dept 2021 Regulation
Basic Electrical and Electronics Engineering
BE3251 2nd semester Mechanical Dept | 2021 Regulation | 2nd Semester Mechanical Dept 2021 Regulation
Physics for Civil Engineering
PH3201 2021 Regulation | 2nd Semester Civil Dept 2021 Regulation
Basic Electrical, Electronics and Instrumentation Engineering
BE3252 2021 Regulation | 2nd Semester Civil Dept 2021 Regulation
Physics for Information Science
PH3256 2nd Semester CSE Dept | 2021 Regulation | 2nd Semester CSE Dept 2021 Regulation
Basic Electrical and Electronics Engineering
BE3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation