GATE Computer ScienceHardGATE CS (PYQ pattern)

Assume char is 1 byte, int is 4 bytes with 4-byte alignment, double is 8 bytes with 8-byte alignment, and the compiler inserts only the padding alignment requires. struct S { char c; double d; int i; } arr[5]; struct S *p = &arr[1], *q = &arr[4]; char *r = (char *)q, *s = (char *)&p->i; printf("%td %td %zu", q - p, r - s, sizeof(struct S)); What is printed?

  1. A. 3 30 13
  2. B. 3 56 24
  3. C. 3 44 20
  4. D. 72 56 24

Correct answer: B. 3 56 24

c sits at offset 0, d must start at 8 (7 bytes padding), i at 16, and the size is rounded up to a multiple of 8: 24. q - p counts elements: 3. q starts at byte 4 * 24 = 96 of arr and p->i at 24 + 16 = 40, so r - s = 56 bytes. Ignoring padding gives size 13; forgetting the tail padding gives 20.

More Programming and Data Structures (GATE) questions

Exams that ask this topic