CKTIS / C
Function pointer declarator
Declare function pointers, read the grouping parentheses, and recognize functions that return these pointers.
Version: C23; repository compiler flag, production runtime not attested
Syntax
int (*fp)(int, int);
int (*select_op(void))(int, int);int (*fp)(int, int); declares a pointer to a function taking two int arguments and returning int. Parentheses matter: int *fp(int, int); declares a function returning int *.
int (*select_op(void))(int, int); declares a parameterless function returning such a pointer. C cannot return functions directly. An alias simplifies this: typedef int (*operation)(int, int);, followed by operation select_op(void);.
In the example, add converts to a pointer. Calls fp(2, 3) and (*fp)(2, 3) are equivalent. Do not call a null pointer or call a function through an incompatible pointer type: the behavior is undefined.
Reference identifiers covered by this entry: function pointer declarator, function returning a function pointer.
Examples
#include <stdio.h>
static int add(int a, int b)
{
return a + b;
}
static int (*select_op(void))(int, int)
{
return add;
}
int main(void)
{
int (*fp)(int, int) = select_op();
printf("%d\n", fp(2, 3));
return 0;
}
Result
5
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.