First time here? Checkout the FAQ!
x
menu search
brightness_auto
more_vert

Distinguish between structure and union in c

thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike

2 Answers

more_vert
 
verified
Best answer
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
We can access all members of structure at a time.
We can access only one member of union at a time.
4
 
Structure example:
 
struct student
{
int mark;
char name[6];
double average;
};
 
Union example:
 
union student
{
int mark;
char name[6];
double average;
};
 5
 
For above structure, memory allocation will be like below.
 
int mark – 2B
char name[6] – 6B
double average – 8B
Total memory allocation = 2+6+8 = 16 Bytes
 
For above union, only 8 bytes of memory will be allocated since double data type will occupy maximum space of memory over other data types.
 
Total memory allocation = 8 Bytes
thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike
more_vert

Structure Union
We use the struct statement to define a structure. We use the union keyword to define a union.

thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike
...