Consider the following C declaration Cstruct { short s[5]; union { float y;…
2015
Consider the following C declaration
struct
{
short s[5];
union
{
float y;
long z;
}
u;
}t;
Assume that the 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 consideration, is
- A.
22 bytes
- B.
18 bytes
- C.
14 bytes
- D.
10 bytes
Attempted by 316 students.
Show answer & explanation
Correct answer: B
The struct contains an array of five shorts (5 × 2 bytes = 10 bytes). The union member holds either a float (4 bytes) or a long (8 bytes), so the union size is determined by its largest member, which is 8 bytes. Ignoring alignment considerations as instructed, the total memory requirement is simply the sum of these parts: 10 bytes plus 8 bytes equals 18 bytes.