GATE / CS ARCHIVE

SUBJECT COLLECTION

Programming in C · Structures & Declarations

4 questions, newest first

GATE CSE has asked 4 questions on Structures & Declarations (Programming in C) from 2000 to 2005, in 2 of the 46 papers; the most recent is in GATE CSE 2005. In the last five years it carried 0 marks per 100-mark paper on average, against 0.1 since 2003.

Browse topics
1 mark · MCQ
Report issue

Reading a function-pointer declaration

Structures & Declarations

Question 1, GATE 2005: Interpret C declaration int (*f)(int *). A function taking int* and returning int; B function taking int and returning int*; C pointer to function taking int* and returning int; D function taking int* and returning a function pointer.
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

In int (*f)(int *), the parentheses make f a pointer. Its target is a function taking one int-pointer argument and returning int. Thus C; it is not a function returning a pointer.

Read outward from f. The parentheses bind *f before the following parameter list, so f is a pointer. Its pointed-to object is a function whose parameter is int* and whose return type, written at the left, is int. Without parentheses, int *f(int*) would instead declare a function returning int*. That binding distinction gives C.

Original paper, page 1

2 marks · MCQ
Report issue

Removing a function prototype

Structures & Declarations

Question 32, GATE 2005: Program declares double foo(double) before main. main declares double da, db, obtains da through omitted input code, and assigns db=foo(da). Later definition double foo(double a){return a;}. If the initial declaration is removed, choose A no diagnostic; B harmless warnings; C type-mismatch warnings with unintended results; D compile errors. Distinguish standard-required diagnostic from implementation severity.
Question 32, GATE 2005: Program declares double foo(double) before main. main declares double da, db, obtains da through omitted input code, and assigns db=foo(da). Later definition double foo(double a){return a;}. If the initial declaration is removed, choose A no diagnostic; B harmless warnings; C type-mismatch warnings with unintended results; D compile errors. Distinguish standard-required diagnostic from implementation severity.
Tap to enlarge
Your answer
Answer & solution

Answer: D in ordinary C compilation (no official key was published)

Without the declaration, foo is called before its type is known. Modern C requires a declaration; historical implicit-int rules would assume an int-returning function, which conflicts with the later double-returning definition. A diagnostic is therefore required, and conventional compilers reject the conflicting types, D. C specifies required diagnostics rather than an absolute distinction between a warning and a fatal error, so exact compiler behavior depends on mode/extensions. The commented input of da is understood as omitted input code, as the prompt intends.

The early declaration tells the compiler that the call returns double. Removing it means a modern C compiler encounters an undeclared function call; older implicit-int conventions instead assume int, which conflicts with the later double definition. A diagnostic is required, and ordinary compiler modes reject the conflicting types, making D the conventional choice. The standard does not mandate the user-interface label warning versus error, so that distinction remains qualified.

Original paper, page 4

1 mark · MCQ
Report issue

Reading an array-of-pointers declaration

Structures & Declarations

Question 1.11, GATE 2000: Given struct node with int i and float j, followed by struct node *s[10], what is s: array of structure pointers, two-field structure of arrays, three-field structure, or array of structures?
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

Step 1: Read outward from s. The immediately attached [10] makes s an array of ten elements.

Step 2: The preceding * says each element is a pointer, and the base type struct node says what it points to. Thus s is an array of ten pointers to nodes, A.

The node’s two fields describe each pointed-to object, not extra fields inside s. Writing struct node s[10] without * would instead produce the array of structures in D.

Original paper, page 2

1 mark · MCQ
Report issue

Structure containing an array and a union

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