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?
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):sis of typeshort, so dereferencing*sis an invalid indirection error. - (B)
i = f(i, s):freturnsvoid, so assigning its result toint iis an illegal assignment error. - (C)
f(i, *s): Dereferencing non-pointer*sis an invalid indirection error. - (D)
f(i, *p):iis anint, and*pdereferencesshort *to yield ashort. These exactly match the parameter types(int, short), and thevoidreturn value is correctly discarded.
Therefore, option D will NOT result in a type checking error.
D
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.