Consider the following C declaration
struct {
short s[5];
union {
float y;
long z;
}u;
} t;
Assume that objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes, respectively. The memory requirement for variable t, ignoring alignment considerations, is
Answer: C
Correct answer: 18 bytes
Add the structure’s members, taking the union as its largest member:
| short s[5] | 5 × 2 = 10 bytes |
|---|---|
| union {float; long} | max(4, 8) = 8 bytes |
| total | 18 bytes |
A structure sums its members; a union takes only the largest of its own.
Please log in or register to report this question.