Learn C Programming

Lesson 6 of 10 · Pointers and Memory

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.

Checking what this lesson needs for you…

What is inside Function Pointers: Passing Behaviour Around

  1. A function lives at an address too
  2. Declaring a function pointer, read aloud
  3. A callback: a function handed to another function
  4. qsort: one sort, any order
  5. A comparator must not subtract
  6. Sorting words: the comparator calls strcmp
  7. typedef, and a dispatch table that replaces the switch