In C, which of the following function declarations correctly defines a function that takes a pointer to a float and a pointer to a pointer to a char, and returns a pointer to a pointer to an int?

Difficulty: Medium

Correct Answer: int **fun(float *pf, char **ppc);

Explanation:


Introduction / Context:
Pointers and pointer declarations in C can be confusing, especially when multiple levels of indirection are involved. Interview questions often test your ability to read and write function prototypes that use pointers correctly. This question asks you to identify a function declaration where the function returns a pointer to a pointer to an int and takes two parameters: a pointer to a float and a pointer to a pointer to a char.


Given Data / Assumptions:

  • The return type must be int **, which is a pointer to a pointer to an int.
  • The first parameter must be float *, a pointer to float.
  • The second parameter must be char **, a pointer to a pointer to char.


Concept / Approach:
In C, the placement of asterisks around the function name and parameter names determines the levels of indirection. int **fun(...) means that fun returns a value of type int **. Inside the parentheses, float *pf declares a parameter pf that is a pointer to float, and char **ppc declares ppc as a pointer to a pointer to char. Options that change the number of asterisks in either the return type or parameter types no longer match the requirements given in the question.


Step-by-Step Solution:
Step 1: Start from the required return type: pointer to pointer to int, which is written as int **.Step 2: Place the function name fun immediately after this: int **fun(...).Step 3: For the first parameter, a pointer to float is written as float *pf, inside the parameter list.Step 4: For the second parameter, a pointer to pointer to char is written as char **ppc.Step 5: Option A, int **fun(float *pf, char **ppc);, matches this structure exactly. Options B, C and D change either the return type or the syntax, so option A is correct.


Verification / Alternative check:
You can verify the interpretation by reading the declaration from the variable names outward. In option A, ppc is a pointer to a pointer to char, which is consistent with char **. pf is a pointer to float, consistent with float *. The function name fun is preceded by int **, which clearly indicates that the result of calling fun is an int **. Compilers accept this valid declaration and treat the return type and parameters according to these indirections.


Why Other Options Are Wrong:
Option B changes both the return type and second parameter type; it returns int * instead of int ** and takes char * instead of char **. Option C returns int *** (pointer to pointer to pointer to int), which is a different type than requested. Option D uses invalid syntax such as *float and **char in the parameter list, which does not conform to C declaration rules.


Common Pitfalls:
Students often misplace asterisks or forget that the number of stars indicates the level of indirection. Another pitfall is to misread a declaration by starting from the left and not grouping the asterisks correctly with the variable or function name. A useful tip is to identify the base type first, then count the stars next to the name. Practice with examples like this helps build confidence in reading and writing pointer rich function prototypes.


Final Answer:
int **fun(float *pf, char **ppc);

Discussion & Comments

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