Skip to content
UPEEC

Function Pointers

Store the address of a function in a variable — for callbacks, state machines and interrupt tables.

Advanced
Comments 0
void blink(void)  { /* ... */ }
void beep(void)   { /* ... */ }

void (*action)(void) = blink;   // pointer to a function taking/returning nothing
action();                        // calls blink

void (*handlers[])(void) = { blink, beep };
handlers[1]();                   // calls beep

Function pointers implement callbacks (qsort takes a comparison function), table-driven state machines and the interrupt vector table on microcontrollers.

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.