Machine-translated by Google. Questions are authored in English.
Top

Discussion

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

  • A.22 bytes
  • B.14 bytes
  • C.18 bytes
  • D.10 bytes

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
total18 bytes

A structure sums its members; a union takes only the largest of its own.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.