Question

Consider the following C program.

void f(int, short);
void main()
{
    int i = 100;
    short s = 12;
    short *p = &s;
    __________ ; // call to f()
}

Which one of the following expressions, when placed in the blank above, will NOT result in a type checking error?

Choose one option
Official answer and solution
Official answer
D
From
Official GATE 2016 answer key

Solution

Check C function call types against the function declaration prototype.

The function declaration is:
void f(int, short);
This states that f accepts an int as the first argument, a short as the second argument, and returns void.

The variables in main are declared as:

  • int i = 100; (type: int)
  • short s = 12; (type: short)
  • short *p = &s; (type: short *)

Evaluate each option:

  • (A) f(s, *s): s is of type short, so dereferencing *s is an invalid indirection error.
  • (B) i = f(i, s): f returns void, so assigning its result to int i is an illegal assignment error.
  • (C) f(i, *s): Dereferencing non-pointer *s is an invalid indirection error.
  • (D) f(i, *p): i is an int, and *p dereferences short * to yield a short. These exactly match the parameter types (int, short), and the void return value is correctly discarded.

Therefore, option D will NOT result in a type checking error.

Final answer

D

Common trap

Missing that s is a variable of type short rather than a pointer, so *s causes a compile-time indirection error.

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2016 Computer Science and Information Technology (CS) Session 1
Question
22 of 65, printed as CS Q.12
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.