A union is a special data type available in C that allows to store different data types in the same memory location.
You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple-purpose.
To define a union, you must use the union keyword in the same way as you did while defining a structure. The union statement defines a new data type with more than one member for your program.
For example
Syntax:
|
Example:
|
union union_name
{
data_type member1;
data_type member2;
.
data_type memeber;
}union_variable;
|
union Data
{
int i;
float f;
char str[20];
}d;
|
Now, a variable of Data type can store an integer, a floating-point number, or a string of characters. It means a single variable, i.e., same memory location, can be used to store multiple types of data. You can use any built-in or user defined data types inside a union based on your requirement.
The memory occupied by a union will be large enough to hold the largest member of the union.
For example, in the above example, Data type will occupy 20 bytes of memory space because this is the maximum space which can be occupied by a character string.
To access any member of a union, we use the member access operator (.).
While accessing union, we can have access to single data member at a time. we can access single union member using following two Operators
- Using DOT (.) Operator (also known as ember access operator)
- Using ARROW(->) Operator (with union and pointer)
The member access operator is coded as a period between the union variable name and the union member that we wish to access. You would use the keyword union to define variables of union type.
#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str[20];
};
void main( )
{
union Data d; //union variable
clrscr();
data.i = 10;
printf( "d.i : %d\n", d.i);
data.f = 220.5;
printf( "d.f : %f\n", d.f);
strcpy( d.str, "C Programming");
printf( "d.str : %s\n", d.str);
getch();
}
|
Structure allocates storage space for all its members separately. Whereas, Union allocates one common storage space for all its members.
We can access only one member of union at a time. We can’t access all member values at the same time in union.
Many union variables can be created in a program and memory will be allocated for each union variable separately.
Difference between structure and union in C:
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
|
Structure allocates storage space for all its members separately
|
Union allocates one common storage space for all its members
|
4
|
We can access all members of structure at a time.
|
We can access only one member of union at a time.
|
5
|
Structure example:
struct student { int mark; char name[10]; float avg; }; |
Union example:
union student { int mark; char name[10]; float avg; }; |
6
|
For above structure, memory allocation will be like below.
int mark – 2B char name[10] – 10B float avg – 4B Total memory allocation = 2+10+4 = 16 Bytes |
For above union, only 10 bytes of memory will be allocated since char data type will occupy maximum space of memory over other data types.
Total memory allocation = 10 Bytes |
0 comments:
Post a Comment