Union
-
Upload
frijo-francis -
Category
Education
-
view
167 -
download
0
Transcript of Union
![Page 1: Union](https://reader038.fdocuments.us/reader038/viewer/2022110219/54be30b44a795949228b461c/html5/thumbnails/1.jpg)
UNION
popo
![Page 2: Union](https://reader038.fdocuments.us/reader038/viewer/2022110219/54be30b44a795949228b461c/html5/thumbnails/2.jpg)
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
![Page 3: Union](https://reader038.fdocuments.us/reader038/viewer/2022110219/54be30b44a795949228b461c/html5/thumbnails/3.jpg)
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
![Page 4: Union](https://reader038.fdocuments.us/reader038/viewer/2022110219/54be30b44a795949228b461c/html5/thumbnails/4.jpg)
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)
![Page 5: Union](https://reader038.fdocuments.us/reader038/viewer/2022110219/54be30b44a795949228b461c/html5/thumbnails/5.jpg)
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)