ISRO Scientist/Engineer CSE 2020Programming in C › Not yet assigned to a topic
ISRO Scientist/Engineer CSE 2020 PYQ: Following declaration of an array of struct, assumes size of byte, short, int and long are 1,2,3 and 4…
Following declaration of an array of struct, assumes size of byte, short, int and long are 1,2,3 and 4 respectively. Alignment rule stipulates that $n$ – byte field must be located at an address divisible by $n$, the fields in the struct are not rearranged, padding is used to ensure alignment. All elements of array should be of same size.
Struct complx
Short s
Byte b
Long l
Int i
End Complx
Complx C[10] Assuming $C$ is located at an address divisble by 8, what is the total size of $C$, in bytes?
- A150
- B160Correct answer
- C200
- D240
Answer
(B) $160$
Approach
Place the fields with the given sizes (short=2, byte=1, long=4, int=3): short s at offset 0-1, byte b at 2, one padding byte at 3, long l at 4-7 (needs address divisible by 4), then int i must sit at an address divisible by 3, so offset 8 is padded and i occupies 9-11. That gives a 12-byte payload, which must then be padded so that every array element is the same size and correctly aligned; with the array base required to be 8-divisible (as stated for C), each element is rounded up to a 16-byte stride. Hence each element = 16 bytes and the array C[10] = 16 × 10 = 160 bytes, matching option B. The naive 12×10=120 value is not among the choices, confirming the intended element size is the padded 16 bytes.
Reviewed by ISRO IQ Editorial Team.