ProgrammingHardProduct company OA · GATE CS

Assume 4-byte int, 8-byte double and natural alignment. What is printed?

Assume 4-byte int, 8-byte double and natural alignment. What is printed?
struct T { double d; int i; };
struct T arr[3];
printf("%td", (char *)(arr + 2) - (char *)arr);
  1. A. 26
  2. B. 24
  3. C. 2
  4. D. 32

Correct answer: D. 32

struct T needs 12 bytes of members but is padded to 16 so that the double in the next array element stays 8-byte aligned. arr + 2 is two elements on: 2 × 16 = 32 bytes. '24' forgets the tail padding; '2' forgets the char casts.

More C Programming questions

Exams that ask this topic