Module 11 · Pointers and Memory
Function Pointers: Passing Behaviour Around
ProReading
Zara's weather station logs how far each forecast missed the real temperature: -3, 7, 0, -12, 5 and 3 degrees. She wants the list three ways: smallest first, largest first, and closest to zero first. Three sort functions would copy one loop three times, changing a single comparison. Zara would rather hand one sort the comparison, the way she hands a function a number. C allows exactly that, because a function has an address too.
Pro lesson
This one goes deeper
It is included with this track and with Learn Pro. Every question on the Skill Test can be answered from the free lessons, so this is depth and never a gate on the certificate.
What is inside Function Pointers: Passing Behaviour Around
- A function lives at an address too
- Declaring a function pointer, read aloud
- A callback: a function handed to another function
- qsort: one sort, any order
- A comparator must not subtract
- Sorting words: the comparator calls strcmp
- typedef, and a dispatch table that replaces the switch