QUESTION 42 Which one of the given options is correct with respect to the return statement...
Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.
QUESTION 42
Which one of the given options is correct with respect to the return statement of the function declaration given below?
int h (int (* s) (int, int), int x, int y);
Show answer & explanation
The function h returns an int and receives a function pointer s (which takes two int parameters and returns an int) plus two int parameters x and y. The correct return statement must dereference and invoke the function pointer with the two arguments. Option D correctly uses (* p) (x, y) to call the function pointed to by p with arguments x and y, which returns an int. Options A and B are syntactically invalid (dereferencing a function pointer or mixing syntax incorrectly), and Option C returns a hardcoded value rather than invoking the function pointer.
Step-by-step Derivation:
Analyze the function signature:
int h (int (* s) (int, int), int x, int y);- Return type:
int - Parameter 1:
int (* s) (int, int)— a pointer to a function taking twoints and returningint - Parameter 2:
int x— integer - Parameter 3:
int y— integer
- Return type:
To return an
int, we must either:- Return a constant integer, OR
- Call the function pointer
s(orpin the options) with the appropriate arguments
Evaluate each option:
- A)
return *p;— Dereferencing a function pointer gives the function itself, not a valid return value of typeint. - B)
return (* p) (x, y) (*x, *y);— Syntax error; mixing function call with invalid dereference of integers. - C)
return 0;— Valid syntax but ignores the function pointer parameter; not the intended use. - D)
return (* p) (x, y);— Correctly dereferencesp, invokes it with argumentsxandy, and returns the resultingint.
- A)
Correct answer: D