C declarations: write a declaration for "A pointer to a function which receives nothing and returns nothing"
-
Avoid *(ptr)*int;
-
Bvoid *(*ptr)()
-
Cvoid *(ptr)()
-
Dvoid (*ptr)()
Answer
Correct Answer: void (*ptr)()
Explanation
Introduction / Context:This item checks whether you can construct a function pointer type for a procedure that takes no arguments and returns no value. In modern C, an explicit (void) parameter list indicates no parameters; older code sometimes uses empty parentheses, which historically meant an unspecified parameter list. Both styles appear in real code and documentation.
Given Data / Assumptions:
- Pointer to a function is required.
- The function returns nothing (void).
- The function takes no parameters.
- No qualifiers or calling conventions are specified.
Concept / Approach:
The scheme for function pointers is return_type (*identifier)(parameters). We therefore place the pointer declarator around the identifier with parentheses, and supply the parameter list. Using an empty list () is commonly seen; the most explicit modern form is (void) to declare no parameters. Thus, void (*ptr)() is acceptable, and void (*ptr)(void) is even clearer where supported by your coding standard.
Step-by-Step Solution:
1) Choose return type: void. 2) Make a pointer to a function: (*ptr). 3) Provide parameter list: () or (void). 4) Final form: void (*ptr)().Verification / Alternative check:
With typedefs: typedef void proc_t(void); then proc_t *ptr; has the same meaning as void (*ptr)(void). Practical tests, like assigning ptr = &some_function; and invoking (*ptr)(); confirm correct behavior.
Why Other Options Are Wrong:
void *(ptr)*int; — Invalid grammar; mixes pointer declarators and parameters incorrectly.
void *(*ptr)() — Declares a function pointer returning void*, not void.
void *(*ptr)(*) — Not valid C syntax; parameter list cannot be a bare *.
Common Pitfalls:
- Omitting parentheses around *ptr, which changes the declaration's meaning.
- Confusing returning void with returning void*; these are completely different types.
Final Answer:
void (*ptr)()