What is the output of the following C++ program that calls a function through a function pointer? #include <iostream> using namespace std; int n(char c, int i); int (*p)(char, int) = n; int main() { (*p)('d', 9); p('d', 9); return 0; } int n(char c, int i) { cout << c << i; return 0; }

Difficulty: Medium

Correct Answer: d9d9

Explanation:


Introduction / Context:
This program demonstrates the use of a function pointer in C++ to call a function in two different ways. It is a common style of interview question because it checks your understanding of function pointer syntax, how function names relate to pointers, and what actually gets printed when the function is called through the pointer. The code calls the same function twice, so recognising that both calls produce the same output is key to answering correctly.


Given Data / Assumptions:

  • The function n takes a char and an int, prints them using cout, and returns an int.
  • A function pointer p of type int (*)(char, int) is initialised with the address of n.
  • In main, the function is invoked first as (*p) with arguments and then as p with arguments.
  • We assume that the program compiles successfully without errors.


Concept / Approach:
In C++, a function name such as n can decay to a pointer to the function, and assigning n to a function pointer p with the correct type is allowed. Calling a function through a pointer can be done using either the explicit dereference syntax (*p)(arguments) or simply p(arguments); both forms are equivalent. Each call to n prints the character argument followed by the integer argument with no space between them. Since the same arguments are used in both calls, the two outputs are identical and appear back to back in the output stream.


Step-by-Step Solution:
Step 1: Examine the function prototype: int n(char c, int i). The function prints c and i and then returns 0. Step 2: Look at the pointer declaration int (*p)(char, int) = n; which creates a pointer to a function with the same signature and assigns it the address of n. Step 3: In main, the first call is (*p)('d', 9); which invokes n with c equal to the character d and i equal to 9, so it prints the character d followed by the digit 9, giving d9. Step 4: The second call p('d', 9); is simply a shorter syntax for dereferencing the pointer and calling the same function with the same arguments, so it again prints d9. Step 5: Because the two outputs are concatenated with no spaces or newlines, the final result on the screen is d9d9.


Verification / Alternative check:
You can verify that (*p)(...) and p(...) are equivalent by consulting C++ language references, which state that both forms are valid and call the function pointed to by p. If you modify the function body to insert a marker before the print, such as printing a symbol each time n is entered, you will see that n is entered twice. This confirms that the function executes twice with the same arguments, and therefore prints the same d9 sequence two times, yielding d9d9 overall.


Why Other Options Are Wrong:
Option d99: This would suggest that only one d is printed and that the two integers are combined into 99, which does not match the function body that prints c and i each time in order. Option d9: This output corresponds to only one call to n, but the code clearly calls the function twice. Option compile time error: The code uses correct function pointer syntax and a matching signature, so there is no reason for a compilation failure under standard C++ rules.


Common Pitfalls:
Some learners mistakenly believe that you must always dereference a function pointer explicitly and think that p(arguments) is not allowed, which can lead them to predict an error incorrectly. Others forget that the function is called twice and therefore only account for one d9 sequence. Another source of confusion is mixing up the type of the pointer and the arguments; however, in this example they match exactly, so the code behaves in a straightforward way. Careful reading and stepwise tracing of each call help avoid these mistakes.


Final Answer:
The program prints d9 for each of the two calls to n, so the complete output is d9d9.

Discussion & Comments

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