Sobes.tech
Junior — Middle

How are function pointer declarations made in programming languages?

sobes.tech AI

Answer from AI

In C/C++, a function pointer is declared by specifying the return type, the pointer's name in parentheses with the * symbol, and the list of function parameters in parentheses.

Example of declaring a pointer to a function that takes two int and returns an int:

int (*funcPtr)(int, int);

Here, funcPtr is a pointer to a function, which can be called as:

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

funcPtr = sum;
int result = funcPtr(2, 3); // result will be 5

Thus, the syntax is:

<return_type> (*<pointer_name>)(<parameter_list>);