Which of the following is correct way to declare a functional pointer in C?

2024

Which of the following is correct way to declare a functional pointer in C?

  1. A.

    int *func (int, int);

  2. B.

    int (*func) (int, int);

  3. C.

    int (func*) (int, int);

  4. D.

    int *func* (int, int);

Attempted by 235 students.

Show answer & explanation

Correct answer: B

Correct declaration: int (*func) (int, int);

Why this is correct: Parentheses around the * and the identifier ensure the declarator is a pointer to a function that takes two int arguments and returns an int. Without those parentheses, the declaration means something else (for example, a function returning a pointer).

  • Example function definition: int add(int a, int b) { return a + b; }

  • Declare and assign a function pointer: int (*func_ptr)(int, int) = add;

  • Call through the pointer: int result = func_ptr(2, 3);

Notes on the other forms:

  • int *func (int, int); declares a function that returns an int pointer (a function returning int*), not a pointer to a function.

  • int (func*) (int, int); and int *func* (int, int); are invalid C syntax and will not compile.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Mppsc Assistant Professor