Which of the following can store the address of a function in C++?

int add(int a, int b)
{
    return a + b;
}

int (*ptr)(int, int) = add;
                        

Options

  • A. Function pointer
  • B. Integer variable only
  • C. Array only
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

A. Function pointer

Detailed Explanation

Answer: (A) Function pointer

A function pointer is a pointer that can store the address of a function.

In the example, ptr can store the address of the add() function.

The function pointer can subsequently be used to call the function.

Function pointers are useful when a program needs to pass a function as an argument or select a function dynamically.