Consider the following C program. void f(int, short); void main() { int i =…
2016
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?
- A.
\(f(s,*s)\) - B.
\( i = f(i,s)\) - C.
\(f(i,*s)\) - D.
\(f(i,*p)\)
Attempted by 211 students.
Show answer & explanation
Correct answer: D
Answer: f(i,*p) does not produce a type checking error.
Reasoning and quick checks:
Function prototype: f expects (int, short).
f(s,*s): The first argument s (short) can convert to int, but *s tries to dereference a short (s is not a pointer) — this is a type error.
i = f(i,s): The call f(i,s) matches parameter types, but f returns void. Assigning a void result to i is a type error. If written as f(i,s); without assignment, it would be fine.
f(i,*s): *s is invalid for the same reason as above; s is not a pointer, so dereferencing it causes a type error.
f(i,*p): p is a short*, so *p yields a short. The first argument i is int. Both argument types match the prototype, so this call is type-correct.
A video solution is available for this question — log in and enroll to watch it.