ccompiler.inOnline C Compiler & Docs
C Example Code2026-03-252 min read

Master function pointers in C with syntax rules, callback function handlers, comparator sorting, and jump tables with full runnable code.

What is a Function Pointer?

In C, functions reside in executable code memory. A Function Pointer holds the starting memory address of an executable function, enabling dynamic dispatch, callbacks, and polymorphic programming patterns.

Declaration Syntax:

c (ISO Standard)
return_type (*pointer_name)(parameter_types);

C Source Code: Callback & Dispatcher

c (ISO Standard)
#include <stdio.h>

int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }
int multiply(int a, int b) { return a * b; }

/* Function taking a callback function pointer */
void executeOperation(int a, int b, int (*operation)(int, int), const char *name) {
    int result = operation(a, b);
    printf("Operation [%s] on (%d, %d) = %d\n", name, a, b, result);
}

int main() {
    int x = 20, y = 5;

    /* 1. Direct function pointer usage */
    int (*mathFunc)(int, int) = add;
    printf("Direct call via pointer: %d\n", mathFunc(x, y));

    /* 2. Passing function pointers as callbacks */
    executeOperation(x, y, add, "ADDITION");
    executeOperation(x, y, subtract, "SUBTRACTION");
    executeOperation(x, y, multiply, "MULTIPLICATION");

    /* 3. Array of function pointers (Jump Table) */
    int (*opsTable[])(int, int) = {add, subtract, multiply};
    printf("Jump Table Result [index 2]: %d\n", opsTable[2](x, y));

    return 0;
}

Sample Output

text (ISO Standard)
Direct call via pointer: 25
Operation [ADDITION] on (20, 5) = 25
Operation [SUBTRACTION] on (20, 5) = 15
Operation [MULTIPLICATION] on (20, 5) = 100
Jump Table Result [index 2]: 100

Complexity Analysis

  • Time Complexity: O(1) indirect branch instruction.
  • Memory: 8 bytes per function pointer on 64-bit platforms.

Related C Examples & Tutorials