Example 6: Differentiate between a structure and a union. Solution 6: The differences are tabulated as follows:
Structure Union 1. Structures are used to store different members at different
places in memory.
1. A union is used to store different members at the same memory
location.
2. They are not used to conserve memory.
2. They are used to conserve memory.
3. Total memory used is equal to the sum of the sizes of its
individual elements/members.
3. Total memory assigned is equal to the maximum/largest of all of
the sizes of its members.
4. The keyword struct is used.
4. The keyword union is used.
5. Syntax:
struct {
member-1;
member-2;
………
member-n;
};
5. Syntax:
union {
member-1;
member-2;
………
member-n;
};
Example