Function Pointer Tables
Dispatch patterns.
Function Pointer Tables is a free C Academy lesson on CoddyKit. This is lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 4 lessons in total.
Tables of Functions
You can store function pointers in an array to build a dispatch table. An index or code selects which function to run, replacing long if-else or switch chains.
#include <stdio.h>
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
int main(void) {
int (*ops[2])(int,int) = { add, sub };
printf("%d\n", ops[0](5, 3));
printf("%d\n", ops[1](5, 3));
return 0;
}Indexing the Table
Pick an operation at runtime by indexing the array. The index can come from user input or program state.
#include <stdio.h>
int mul(int a, int b) { return a * b; }
int add(int a, int b) { return a + b; }
int main(void) {
int (*ops[2])(int,int) = { add, mul };
int choice = 1;
printf("%d\n", ops[choice](6, 7));
return 0;
}Replacing a switch
A dispatch table is often cleaner than a switch when each case just calls a function.
#include <stdio.h>
void start(void) { printf("start\n"); }
void stop(void) { printf("stop\n"); }
void pause(void) { printf("pause\n"); }
int main(void) {
void (*table[3])(void) = { start, stop, pause };
for (int i = 0; i < 3; i++) table[i]();
return 0;
}typedef Tidies the Table
Using a typedef for the function pointer type makes the table declaration short and readable.
#include <stdio.h>
typedef int (*BinOp)(int,int);
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
int main(void) {
BinOp ops[] = { add, sub };
printf("%d\n", ops[0](10, 4));
return 0;
}Mapping Symbols to Functions
Pair each operator symbol with its function in parallel arrays or a struct table to look up by name.
#include <stdio.h>
typedef int (*BinOp)(int,int);
int add(int a, int b){return a+b;}
int sub(int a, int b){return a-b;}
int main(void) {
char syms[] = {'+', '-'};
BinOp ops[] = { add, sub };
char want = '-';
for (int i = 0; i < 2; i++)
if (syms[i] == want) printf("%d\n", ops[i](9, 2));
return 0;
}A Struct-Based Command Table
Bundle a name and a handler in a struct, then make an array of them. This is the classic command table pattern.
#include <stdio.h>
#include <string.h>
typedef struct {
const char *name;
void (*run)(void);
} Command;
void hello(void){printf("hi\n");}
void bye(void){printf("bye\n");}
int main(void) {
Command cmds[] = {{"hello", hello}, {"bye", bye}};
const char *input = "bye";
for (int i = 0; i < 2; i++)
if (strcmp(cmds[i].name, input) == 0) cmds[i].run();
return 0;
}Bounds Checking
Always validate the index before calling through the table to avoid reading past the array.
#include <stdio.h>
typedef int (*Fn)(int);
int sq(int x){return x*x;}
int neg(int x){return -x;}
int main(void) {
Fn table[] = { sq, neg };
int n = 2;
int idx = 5;
if (idx >= 0 && idx < n) printf("%d\n", table[idx](3));
else printf("index out of range\n");
return 0;
}State Machines
Dispatch tables drive state machines: each state is a function pointer, and handlers return the next state index.
#include <stdio.h>
int state_a(void){ printf("A\n"); return 1; }
int state_b(void){ printf("B\n"); return -1; }
int main(void) {
int (*states[2])(void) = { state_a, state_b };
int s = 0;
while (s >= 0) s = states[s]();
return 0;
}Plugin-Like Dispatch
By registering handlers in a table, new behavior can be added by appending an entry rather than editing a big switch.
#include <stdio.h>
typedef void (*Handler)(int);
void log_low(int v){printf("low %d\n", v);}
void log_high(int v){printf("high %d\n", v);}
int main(void) {
Handler handlers[] = { log_low, log_high };
int level = 1;
handlers[level](7);
return 0;
}Default Handler
Reserve one slot or a fallback pointer for unknown codes so dispatch never fails silently.
#include <stdio.h>
typedef void (*Fn)(void);
void known(void){printf("known\n");}
void fallback(void){printf("unknown command\n");}
int main(void) {
Fn table[] = { known };
int n = 1, code = 9;
Fn f = (code >= 0 && code < n) ? table[code] : fallback;
f();
return 0;
}Why Tables Scale
Dispatch tables turn branching logic into a data lookup. Adding cases means adding data, which keeps code flat and fast.
#include <stdio.h>
typedef int (*Fn)(int);
int a(int x){return x+1;}
int b(int x){return x*2;}
int c(int x){return x*x;}
int main(void) {
Fn t[] = { a, b, c };
for (int i = 0; i < 3; i++) printf("%d ", t[i](4));
printf("\n");
return 0;
}Quick Check
Test your understanding of function pointer tables.
Recap
You learned dispatch patterns with function pointer tables:
- Store function pointers in an array and select by index.
- They replace long switch chains and scale by adding data.
- Use
typedefand struct command tables for clarity. - Always bounds-check the index and provide a default handler.
Learn C with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 39
- Lessons
- 144
Frequently Asked Questions
Is the “Function Pointer Tables” lesson free?
Yes — the full text of “Function Pointer Tables” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO. The C Academy course includes 4 lessons in total.
What will I learn in “Function Pointer Tables”?
Dispatch patterns. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C Academy?
No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
How long does the “Function Pointer Tables” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C Academy lesson?
Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Declaring Function Pointers
- Passing Functions
- qsort with Comparators
- Function Pointer Tables