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?
View worked solution
Answer: D
The function is declared as:
void f(int, short);
This means f accepts two arguments: the first of type int, the second of type short, and it returns void (no value).
Let's analyze the variables declared in main():
int i = 100;—iis of typeint.short s = 12;—sis of typeshort.short *p = &s;—pis a pointer toshort, so*pis of typeshort.
Now, let's evaluate each option:
- Option A:
f(s, *s)
Here,sis ashortvariable, not a pointer. Applying the dereference operator*to a non-pointer variable (*s) is a compilation/type checking error. - Option B:
i = f(i, s)
The functionfhas a return type ofvoid. Attempting to assign avoidreturn value to anintvariableiresults in a type checking error. - Option C:
f(i, *s)
Similar to Option A,*sis invalid becausesis not a pointer. - Option D:
f(i, *p)
- The first argument
iis of typeint, which matches the first parameter. - The second argument
*pdereferences the pointerp(of typeshort*), yielding a value of typeshort, which matches the second parameter. - The function call is not assigned to any variable, which is correct since
freturnsvoid.
- The first argument
Free practice · Your answers on this page are not saved.
