Union
-
Upload
frijo-francis -
Category
Education
-
view
167 -
download
0
Transcript of Union
UNION
popo
Union• Union is same as the structure• Like structure, union also contains members of different
data type• Union can be declared using the keyword union• Syntax: union unionname{• member 1;• member 2;• . . . . • };• Like structure a union variable can be declare • Syn: union unionname variable list;
popo
Union and Structure• The memory space occupy for a union variable
is the size of largest member• In structure each members has its own memory
locations• In union all members shares a common memory
location• In structure we can access all members at a time • In union we can access only one member at a
time
popo
Union and Structure• Eg • Stuct check• {
int age; char name[5];
• }s;• The memory space of struct variable s=sum of size of all
members• Ie sizeof(age)+sizeof(name)• => 2+5 = 7 bytes• The structure variable has separate memory locations for each
members• Can access all members at a time
popo
age (2 bytes) name (5 bytes)
Union and Structure• Eg • union check• {
int age; char name[5];
• }u;• The memory space of union variable s=size of largest members• Ie sizeof(name)• => 5 = 5 bytes• The union variable has only one memory locations for all
members• Can access only one member at a time
popo
name (5 bytes)