The difference between a structure and a union are:
Structure |
Union |
struct keyword is used to define a structure. |
union keyword is used to define a union. |
Every member within structure is assigned a unique memory
location. |
In union, a memory location is shared by all the data
members. |
It enables you to initialize several members at once. |
It enables you to initialize only the first member of
union. |
The total size of the structure is the sum of the size of
every data member. |
The total size of the union is the size of the largest data
member. |
It is mainly used for storing various data types. |
It is mainly used for storing one of the many data types
that are available. |
It occupies space for each and every member written in
inner parameters. |
It occupies space for a member having the highest size
written in inner parameters. |
Any member can be retrieved at a time. |
One member at a time can be retrieved in the union. |
No comments:
Post a Comment