C declarations: interpret the following declaration int *f(); What exactly does it mean?

Difficulty: Medium

Correct Answer: f is a function returning pointer to an int.

Explanation:


Introduction / Context:
Distinguishing “function returning pointer” from “pointer to function” is a frequent stumbling block in C. The position of parentheses relative to * and the identifier determines whether you declare a function or a pointer to a function. This question focuses on the declaration int *f();


Given Data / Assumptions:

  • Declaration: int *f();
  • Empty parameter list means an unspecified list in old-style C. In modern code, prefer int *f(void) for no parameters.
  • We must decide whether f itself is a pointer or a function.


Concept / Approach:

Operator precedence puts () on the identifier first. Because there are no parentheses around *f, the () applies to f, making f a function. The return type is int*, derived from the leading int *. Therefore, f is a function that, when called, returns a pointer to int. By contrast, a pointer to function returning int would look like int (*pf)();


Step-by-Step Solution:

1) Identify the identifier: f. 2) Postfix () binds to f, so f is a function. 3) The type to the left is int *, therefore the function's return type is pointer to int. 4) Conclude: f is a function returning pointer to int.


Verification / Alternative check:

Introduce a typedef for clarity: typedef int* pint; then pint f(); reads more naturally as “f returns pint”, confirming the interpretation. Another check is to compare with int (*f)(); which would be a pointer to a function returning int, clearly different.


Why Other Options Are Wrong:

Pointer variable of function type: Functions are not objects; you cannot have “pointer variable of function type” as phrased. The declaration does not make f a pointer variable at all.

Function pointer: That would require parentheses around *f: int (*f)();

Simple declaration of pointer variable: A simple pointer variable would be int *p; with no ().


Common Pitfalls:

  • Forgetting that () binds tighter than *, making f a function here.
  • Assuming empty parameter lists mean “no parameters” in all C dialects; use (void) in modern C when you mean no parameters.


Final Answer:

f is a function returning pointer to an int.

Discussion & Comments

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