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?
Correct : F
Given:
Function prototype: void f(int, short);
Variables declared: int i = 100; short s = 12; short *p = &s;
To find:
The expression that will NOT cause a type checking error when placed in the blank.
Solution:
Let me look at the function declaration first.
The function f takes two arguments: the first of type int, and the second of type short.
It returns void, so it cannot be assigned to any variable.
Now let me check the given variables:
i is of type int.
s is of type short.
p is of type pointer to short (short *).
Let me test each option one by one:
Checking Option A: f(s, *s)
Here, s is an integer type (short), not a pointer. Applying the indirection operator * on s (*s) is invalid and causes a type error.
Checking Option B: i = f(i, s)
The function f returns void. We cannot assign a void expression to an int variable i. This will cause a type error.
Checking Option C: f(i, *s)
Again, s is not a pointer, so *s is an invalid operation.
Checking Option D: f(i, *p)
Here, i is of type int, which matches the first parameter.
p is of type short*, so dereferencing it as *p gives a value of type short, which matches the second parameter.
The function call does not attempt to assign the void return value to anything.
So, this call is completely valid and does not produce any type checking error.
Answer: (D)
Similar Questions
Total Unique Visitors