C declarations: interpret the following declaration void *cmp();

Difficulty: Easy

Correct Answer: cmp is a function that return a void pointer.

Explanation:


Introduction / Context:
Distinguishing between “function returning pointer” and “pointer to function” is central to reading C declarations. Here, void *cmp(); uses the function declarator directly on the identifier, with a pointer type as the function's return type. This pattern is common for factory functions that return untyped memory or opaque handles.


Given Data / Assumptions:

  • Declaration: void *cmp();
  • Return type: void* (pointer to void).
  • Parameter list is unspecified in the old-style form; modern code would prefer void *cmp(void).


Concept / Approach:

Because () applies to the identifier cmp (no parentheses around *cmp), cmp is a function, not a pointer variable. The leading void* indicates that the function returns a pointer to void. Therefore the accurate English reading is “cmp is a function that returns a void pointer”.


Step-by-Step Solution:

1) Identify postfix operator: () binds to cmp, so cmp is a function. 2) Inspect the type on the left: void*. 3) Conclude the function's return type is pointer to void. 4) Optionally specify parameters as (void) in modern declarations to mean no parameters.


Verification / Alternative check:

Compare against a pointer-to-function form: void (*cmp)(); would be a pointer to a function returning void, which is different. Typedefs can help: typedef void* anyptr; anyptr cmp(void); reads clearly and confirms the meaning.


Why Other Options Are Wrong:

Pointer to a void type — Incorrect; that would be void *p; and not a function declaration.

Void type pointer variable — Misstates the form; cmp is declared as a function, not a variable.

Returns nothing — That would be return type void, not void*.


Common Pitfalls:

  • Confusing void (no value returned) with void* (pointer return).
  • Assuming empty parameter lists always mean no parameters; in modern C use (void) to be explicit.


Final Answer:

cmp is a function that return a void pointer.

More Questions from Complicated Declarations

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion