GATE / CS ARCHIVE

GATE CSE 2000 / QUESTION 1.17

Structure containing an array and a union

Programming in C · Structures & Declarations · Full GATE CSE 2000 paper

GATE 2000, Q1.17 1 mark · MCQ
Report issue

Structures & Declarations

Question 1.17, GATE 2000: A structure contains short s[5] and a union holding either float y or long z. short,float,long occupy 2,4,8 bytes. Ignoring alignment, choose size 22,14,18,10 bytes.
Question 1.17, GATE 2000: A structure contains short s[5] and a union holding either float y or long z. short,float,long occupy 2,4,8 bytes. Ignoring alignment, choose size 22,14,18,10 bytes.
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

Step 1: The array has five separate short objects, requiring 5·2=10 bytes.

Step 2: Union members share storage, so the union needs the maximum of 4 and 8 bytes, namely 8, rather than their sum. The enclosing structure stores its array and union separately.

Total size ignoring padding is 10+8=18 bytes, C. Adding both union members would incorrectly give 22; actual ABI alignment could add padding, explicitly excluded here.

Original paper, page 2