Coding/Learn/c/c recursion and function pointers
C Foundational Course
01.Introduction, Compilation Pipeline & Syntax
02.Variables, Primitives & Explicit Type Casting
03.Operators, Precedence & Booleans (<stdbool.h>)
04.Control Flow: If-Else, Ternary & Switch
05.Loops (For, While, Do-While) & Jump Statements
06.Contiguous Arrays & Multi-Dimensional Matrices
07.Null-Terminated Strings & <string.h>
08.User Input, scanf & fgets Buffer Safety
09.Pointers, Memory Addresses & Dereferencing
10.Functions, Prototypes & Pass-by-Pointer
11.11. Recursion & High-Speed Function Pointers
12.File Streams: Create, Write & Read (fopen/fclose)
13.Structures, Unions & typedef Aliasing
14.Memory Word Alignment & Struct Padding
15.Enums & Bitwise Manipulation (Flags & Masks)
16.Dynamic Memory: malloc, calloc, realloc & free
17.Defensive Error Handling, errno & Assertions
18.Preprocessor Macros & Storage Classes
19.Fixed-Width Types (stdint.h), Math & Time (<time.h>)
20.Capstone: High-Performance In-Memory Key-Value Engine
Course Certificate0%

Complete all lessons to earn

Learn11. Recursion & High-Speed Function Pointers
11 / 20 0%
Lesson 11 of 20
6 mins read

11. Recursion & High-Speed Function Pointers

PreviousNext Lesson
In Plain English

A function pointer stores the memory address of an executable function in the code text segment. Function pointers enable dynamic dispatch, strategy patterns, and custom comparator callbacks for standard library algorithms like qsort().

The Mental Model

"A regular pointer points to a data record on a shelf; a function pointer points to a button on the wall that executes a specific action when pushed."

Deep Dive: How It Works

Function Pointers in Memory: Functions reside in the executable code segment of process RAM.

Comparator Signature: int (*cmp)(const void *a, const void *b) for standard library qsort.

Recursion Mechanics: Each recursive call adds a new stack frame; base cases are mandatory.

Syntax Blueprint

int (*operation)(int, int) = add;
int result = operation(10, 20);

Declare function pointer matching signature and assign target function address.

PointCore Rules to Remember

Point star
Callback Mechanisms: Enables decoupling algorithm logic from data comparison or event handling.
Point star
qsort Integration: Standard C high-speed quicksort accepting custom void* comparator pointers.
Point star
Zero Overhead: Direct indirect branch machine instruction call without virtual table lookup.

Common Beginner Traps & How to Fix Them

Mistake:Declaring function pointer without parentheses around (*ptrName): int *ptr(int, int).

Why it happens: Without parentheses, this declares a function returning an int pointer rather than a pointer to a function.

How to fix: Always write int (*ptr)(int, int).

Pro Tip: Use typedef to define clean names for complex function pointer signatures: typedef int (*Comparator)(const void *, const void *);

Live Interactive Example

Hit Run Code to see it live
Quicksort with Custom Comparator
C11 / GCC 9.2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Output Console
Click "Run Code" to view the rendered output.
How it works: qsort uses the ascending function pointer callback to sort array elements in-place.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Inspect Sorted Median with qsort

  • PointPrint the sorted median element at index 2 with "Median: %d\n".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates