What are the differences between structures and union?
Ans: A structure variable contains each of the named members, and its size is large enough to hold all the members. Structure elements are of same size.
A union contains one of the named members at a given time and is large enough to hold the largest member. Union element can be of different sizes.
Ans: A structure variable contains each of the named members, and its size is large enough to hold all the members. Structure elements are of same size.
A union contains one of the named members at a given time and is large enough to hold the largest member. Union element can be of different sizes.
Difference Between Stucture and Union :
| Structure | Union |
|---|---|
| i. Access Members | |
| We can access all the members of structure at anytime. | Only one member of union can be accessed at anytime. |
| ii. Memory Allocation | |
| Memory is allocated for all variables. | Allocates memory for variable which variable require more memory. |
| iii. Initialization | |
| All members of structure can be initialized | Only the first member of a union can be initialized. |
| iv. Keyword | |
| 'struct' keyword is used to declare structure. | 'union' keyword is used to declare union. |
| v. Syntax | |
struct struct_name
{
structure element 1;
structure element 2;
----------
----------
structure element n;
}struct_var_nm;
| union union_name
{
union element 1;
union element 2;
----------
----------
union element n;
}union_var_nm;
|
| vi. Example | |
struct item_mst
{
int rno;
char nm[50];
}it;
| union item_mst
{
int rno;
char nm[50];
}it;
|
Blog Author: Vijay Kumar
No comments:
Post a Comment