Loading...

C essentialsPointers & Memory

Function pointers & callbacks

10 min lesson

Next
  • A function pointer is a variable that stores the address of a function, so you can call different functions through the same variable.
  • They matter because they let you pass behavior as data, which is the basis of callbacks, sorting comparators, and simple dispatch tables.
  • Think of a function pointer as a phone number for a service: the number can be swapped, and calling it reaches whichever service is currently stored.
  • The type of a function pointer includes the return type and the parameter types: int (*op)(int, int) points to a function taking two ints and returning an int.
  • Assign a function by name without parentheses: op = add;. Call through the pointer with parentheses: op(2, 3).
  • A callback is a function you pass to another function, which then calls it at the right moment.
  • The computer stores the address of the function's code and jumps there when the pointer is called.
  • The syntax is: returnType (*name)(paramTypes); for example int (*op)(int, int) = add;.
  • A realistic use: qsort's comparator, an event handler, or a menu that maps a choice to a function.
  • A surprising detail: the parentheses around *name are required; without them int *op(int, int) declares a function returning int *.
  • The example program stores two functions in a function pointer, calls each through it, and passes a callback to an apply function.
Keep in mind
  • A function pointer stores the address of a function with a matching signature.
  • Use parentheses in the declaration: int (*fp)(int, int).
  • Assign a function by name and call it with parentheses.
  • Callbacks let you pass behavior to a function, which makes code flexible.
  • This is advanced C, and you handled it well. Great job!
main.c
#include <stdio.h>

// Two simple functions with the same signature
int add(int a, int b) { return a + b; }
int multiply(int a, int b) { return a * b; }

// A function that takes a callback and applies it to two numbers
int apply(int (*op)(int, int), int a, int b) {
    // Call the function through the pointer
    return op(a, b);
}

int main(void) {
    // A function pointer that can point to add or multiply
    int (*op)(int, int) = add;

    // Call add through the pointer
    printf("add via pointer: %d\n", op(3, 4));

    // Reassign the pointer to multiply and call again
    op = multiply;
    printf("multiply via pointer: %d\n", op(3, 4));

    // Pass a function as a callback to apply
    printf("apply add: %d\n", apply(add, 5, 6));
    printf("apply multiply: %d\n", apply(multiply, 5, 6));

    return 0;
}

What happens: add via pointer: 7 multiply via pointer: 12 apply add: 11 apply multiply: 30

Breaking it down

Function definition

int add(int a, int b) { return a + b; }

A normal function. Its name, without parentheses, is its address, which can be stored in a matching function pointer.

Function pointer declaration

int (*op)(int, int) = add;

Declares a pointer to a function taking two ints and returning int, and initializes it to add. The parentheses around *op are required.

Calling through pointer

op(3, 4)

Calls the function the pointer currently refers to. The result is 7 for add and 12 for multiply.

Callback parameter

int (*op)(int, int)

In apply, this parameter accepts any function with that signature, so the caller chooses the behavior.

Passing a function

apply(add, 5, 6)

Passes the address of add as the callback. apply then calls it, giving 11.

Function pointer syntax

FormMeaningExample
int (*fp)(int, int)Pointer to functionDeclaration
fp = addAssign functionNo & needed
fp(2, 3)Call through pointerReturns 5
int (*op)(int, int)Callback parameterIn apply
typedef int (*Op)(int, int)Named aliasEasier to reuse

A helpful analogy

A function pointer is like a phone number for a service. The number (the pointer) stays the same, but it can be swapped to reach a different service. Calling the number is like calling the function through the pointer.

Nesting rules

  • 1.Always write parentheses around the pointer name: int (*fp)(int, int).
  • 2.The function pointer's signature must match the function's return and parameter types exactly.
  • 3.Assign a function by name without parentheses; parentheses would call it instead.
  • 4.Use typedef to make callback types easier to read: typedef int (*Op)(int, int);.

Common mistakes

Omitting parentheses in the declaration

Avoid

int *fp(int, int); // function returning pointer

Use instead

int (*fp)(int, int); // function pointer

Without parentheses, the compiler reads a function that returns int *, not a pointer to a function.

Calling a function with parentheses when assigning

Avoid

op = add(); // calls add

Use instead

op = add;

add() calls the function and stores its result. Assigning add without parentheses stores the function's address.

Calling through a NULL function pointer

Avoid

int (*op)(int, int) = NULL; op(1, 2); // crash

Use instead

if (op != NULL) op(1, 2);

A NULL function pointer points to nothing. Calling it is undefined behavior and usually crashes.

Quick tips

  • •Use typedef to name callback types so signatures do not repeat everywhere.
  • •Check a function pointer for NULL before calling it when it may be unset.
  • •Try adding a subtract function and assigning it to op to see how easy it is.
  • •Print the function pointer with %p (cast to void *) to inspect its address.
  • •Remember that qsort's comparator is a real-world function pointer you can now read.
Previous
Next