A pointer that points to a function instead of data.
typedef int (*MathOperation)(int, int);- Callbacks
- Plugin systems
- State machines
- Sorting with custom comparators (
qsort)
Generic pointers that can point to any data type. You must cast them before dereferencing.
Common in:
malloc()/free()- Generic data structures
- Library APIs
const int *p→ pointer to const int (cannot change the value through this pointer)int *const p→ const pointer (cannot change where the pointer points)const int *const p→ const pointer to const int (neither the pointer nor the value can be changed)
Rule: Use const everywhere you can — it prevents bugs and clearly documents intent.
- Use
typedefto make function pointer types readable - Always document what a function pointer expects
- Be extremely careful with
void*casts - Prefer arrays of function pointers over large
switchstatements
- Write a calculator that uses an array of function pointers for
+,-,*,/. - Implement a simple callback system (e.g., a button press handler).
- Create a generic
swapfunction usingvoid*andmemcpy. - Add function pointers to one of your earlier multi-file examples.