Brainlag
  1. C
  2. Advanced
29/38 lessons

Function pointers

50 min5 exercisesPremium

Store a function's address in a variable, pass callbacks to qsort with a comparator, and build a dispatch table that maps command names to C functions.

The skill
Pass functions around as values: write comparators for qsort, dispatch commands from a table and write your own function that takes a callback.
You make
A sorted leaderboard, a fixed race-time sort, a command table and a pick-the-best function that works on any type.

Premium lesson

£7.99 a month or £59 a year. Cancel any time.

Builds on 28 Hash tables. Free to try: C Foundations and the playground.

Step 1 of 9
exercise_1.c

You've reached the good stuff.

5 exercises in this lesson, checked as you type. Premium opens every lesson after Foundations, in all four courses.

C, 32-bit: int and pointers are 4 bytes