Skip to main content
Be Bitwise
CPointers Deep Dive

Function Pointers

Functions in C live in the text segment, which means each one has an address just like any other byte in memory. A function pointer stores that address, so you can pick which function to call at runtime.

Syntax

Function-pointer syntax is famously horrible:

c
int (*fp)(int, int); // pointer to a function taking two ints, returning int

Read it from the inside out: fp is a pointer (*fp); that pointer aims at a function taking (int, int) and giving back an int. The parens around *fp are doing real work — drop them and you'd declare a function returning a pointer instead.

Using Function Pointers

c
int add(int a, int b) { return a + b; }
int multiply(int a, int b) { return a * b; }
int main() {
int (*operation)(int, int);
operation = add;
printf("%d\n", operation(3, 4)); // 7
operation = multiply;
printf("%d\n", operation(3, 4)); // 12
}

In memory, operation is an 8-byte stack slot like any other pointer. The only thing that makes it special is where it points: into the text segment, at the first instruction of a function. The addresses here are illustrative.

Calling through operation jumps to whatever address is in that slot, no questions asked. That's why attackers love function pointers: overwrite those 8 bytes and the next call goes wherever they chose. The security modules build on exactly this, starting with why ROP exists.

The Callback Pattern

Function pointers are what let C do callbacks. You hand one function to another and let it decide when to invoke yours:

c
void apply(int *arr, int len, int (*fn)(int)) {
for (int i = 0; i < len; i++) {
arr[i] = fn(arr[i]);
}
}
int square(int x) { return x * x; }
int main() {
int nums[] = {1, 2, 3, 4, 5};
apply(nums, 5, square);
// nums is now {1, 4, 9, 16, 25}
}
Key Concept

Callbacks aren't an exotic technique in C, they're a daily tool. qsort, bsearch, atexit, every signal handler — all of them take function pointers. You won't get far in a real codebase without being comfortable here.

Put function pointers in a table next to names and you've got a dispatch table, which is how a lot of command interpreters, opcode handlers and plugin systems are wired.

Try itAdd a sub command: one new function and one new table row. main() doesn't change.
C (GCC 9.2.0)

typedef for Readability

Because the syntax above is awful, most real code reaches straight for typedef:

c
typedef int (*MathOp)(int, int);
int compute(MathOp op, int a, int b) {
return op(a, b);
}

Practical Example: qsort

The classic example: qsort knows nothing about your data. You teach it how to compare two elements via a function pointer.

c
#include <stdlib.h>
int compare_ints(const void *a, const void *b) {
int ia = *(const int *)a, ib = *(const int *)b;
return (ia > ib) - (ia < ib);
}
int main() {
int arr[] = {5, 2, 8, 1, 9};
qsort(arr, 5, sizeof(int), compare_ints);
// arr is now {1, 2, 5, 8, 9}
}

Why (ia > ib) - (ia < ib) and not the shorter ia - ib? Because the subtraction can overflow. The runner below sorts structs by score (highest first) and then by name. Run it, then swap by_score_then_name for by_score_lazy in the qsort call and see what happens to the player stuck on INT_MIN.

Try itSwap in by_score_lazy: where does cat end up now, and why?
C (GCC 9.2.0)

Try It

Drop into the editor and write apply: it takes an array, a length, and a function pointer, then runs that function over every element.

Challenges

Create a function that takes a callback (function pointer) and applies it to each element of an array.

C (GCC 9.2.0)