Functions have addresses too. A function pointer stores that address—enabling callbacks, dispatch tables, and plugin-style APIs.
Syntax
int add(int a, int b) { return a + b; }
int (*op)(int, int) = add;
int result = op(2, 3);
Read declaration inside-out: op is a pointer to function taking two ints and returning int.
typedef for clarity
typedef int (*BinaryOp)(int, int);
BinaryOp op = add;
Important interview questions and answers
- Q: Why function pointers?
A: Runtime polymorphism in C—sort comparators (qsort), event handlers, driver dispatch tables. - Q: NULL function pointer call?
A: Undefined behavior—guard before invoke.
Self-check
- How do you call through a function pointer named op?
- What stdlib function uses a comparison function pointer?
Tip: Use typedef to name function pointer types—callbacks in qsort and event handlers become readable.
Interview prep
- Why function pointers?
Runtime dispatch—sort comparators, callbacks, plugin APIs, and state machines.