C Reference ยท Types and Literals
Function pointers in C
Reference for C function pointers: declaration syntax, typedefs, calling through a pointer, arrays of function pointers, callbacks such as qsort comparators.
A function pointer stores the address of a function so you can call it later, pass it to another function, or keep it in a table. Its type records the return type and the parameter types, so int (*op)(int, int) can point at any function taking two ints and returning an int.
The syntax is awkward because the parentheses around *op are needed: without them, int *op(int, int) declares a function returning int *. A typedef makes function pointer types much easier to read.
int add(int a, int b) { return a + b; } int (*op)(int, int) = add; /* or = &add */ int r = op(2, 3); /* or (*op)(2, 3) */
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
return_type (*name)(param_types); /* pointer to function */ typedef return_type (*alias)(param_types); /* easier to read */ return_type (*name[N])(param_types); /* array of N pointers */ return_type (*getter(args))(param_types); /* function returning one */
| Part | Description |
|---|---|
return_type |
What the target function returns. |
(*name) |
The parentheses make name a pointer rather than a function returning a pointer. |
param_types |
Parameter types; (void) means none. In C23 an empty () also means no parameters, but older standards leave them unspecified. |
Returns: calling through the pointer yields whatever the function returns: name(args) and (*name)(args) are identical.
Errors / undefined behavior: calling through a null or uninitialized pointer, or through a pointer converted to a different function type, is undefined behavior. Converting a function pointer to void * and back is not guaranteed by the standard, though POSIX requires it for dlsym.
Usage notes
- A function name decays to a pointer in most contexts, so
op = addandop = &addare the same, andop(2, 3)and(*op)(2, 3)are the same. - Match the type exactly. A comparator for
qsortmust have the signatureint (const void *, const void *); casting a differently typed function to that type is undefined behavior when called. - Use
typedeffor anything beyond one level. A reader should not have to parsevoid (*signal(int, void (*)(int)))(int). - Initialize and check. A struct of function pointers (a "vtable") should be fully populated or checked against
nullptrbefore calls. - Comparing: pointers to the same function compare equal; ordering them is not meaningful.
- No arithmetic on function pointers, and no
sizeofon a function itself. - Inlining: calls through a pointer are usually not inlined unless the compiler can see the target; this costs a few cycles in hot loops.
- Pointer-to-function members do not exist in C; store a plain function pointer in a struct and pass the object explicitly.