Programming for Problem Solving (3110003)

BE | Semester-1   Winter-2019 | 07-01-2020

Q4) (a)

Distinguish between Structure and Union.

 

Structure Union
Each member is assigned its own unique storage area. All members share the same storage area.
Total memory required by all members is allocated. Maximum memory required by the member is allocated.
All members are active at a time. Only one member is active at a time.
All members can be initialized. Only the first member can be initialized.
Requires more memory. Requires less memory.
Example:
struct SS
{
   int a;
   float b;
   char c;
};
Total bytes = 1 + 2 + 4 = 7 bytes.
Example:
union SS
{
   int a;
   float b;
   char c;
};
4 bytes will be occupied.