Difficulty: Medium
Correct Answer: float (ptr)(int)
Explanation:
Introduction / Context:
This problem assesses your ability to translate precise English descriptions into correct C declarations. Function pointer syntax in C can be tricky because parentheses affect binding order between the pointer declarator * and the function-call operator (). The goal is to declare a pointer to a function that accepts an int pointer as its sole parameter and returns a float pointer (that is, float).
Given Data / Assumptions:
Concept / Approach:
The pattern for a pointer to function is: return_type (*identifier)(parameter_types). Because we want a pointer to function, the * must be parenthesized with the identifier to ensure it binds before the function-call operator (). Then we place the function's parameter list inside the parentheses and put the intended return type to the left of the entire construct. Finally, we specify that the single parameter is a pointer to int (int*), and the function returns float*.
Step-by-Step Solution:
Verification / Alternative check:
Introduce typedefs to simplify reading: typedef float* f_ret_t; typedef f_ret_t func_t(int*); then func_t *ptr; matches float *(*ptr)(int*). Compilers accept both forms equivalently, and typeof or IDE hover info typically confirms the signature.
Why Other Options Are Wrong:
float *(ptr)*int; — Invalid syntax and not a function pointer declaration.
float *(*ptr)(int) — Parameter is int, not int*, so the parameter type is wrong.
float (*ptr)(int) — Returns float, not float*; return type is wrong.
Common Pitfalls:
Final Answer:
float *(*ptr)(int*)
Discussion & Comments