🔴 Advanced  ·  Lesson 35

Function Pointers

What is a function pointer?

You already know a pointer can hold the address of a variable. Here is the surprising part: functions also live at addresses in memory, so a pointer can hold the address of a function too. That is a function pointer.

Why would you want this? Because it lets your program decide at run time which function to call, store functions in arrays, and pass behaviour into other functions. It is the foundation of callbacks and flexible, plugin-style code.

The syntax, broken down

The declaration looks strange at first. Read it in three parts:

C Language
int (*fp)(int, int);
//  ^     ^
//  |     the two parameter types
//  the pointer name, wrapped in ( )
// "int" is the return type

Read as: "fp is a pointer to a function that takes two ints and returns an int." The parentheses around *fp are what make it a function pointer rather than a function that returns a pointer.

⚠️ Don't drop the parentheses

int (*fp)(int) = pointer to a function.   int *fp(int) = a function returning int *. Completely different!

Calling a function through a pointer

Point the function pointer at a real function (just use its name), then call it.

C Language
#include <stdio.h>
int add(int a, int b) { return a + b; }

int main() {
    int (*fp)(int, int) = add;   // point at add
    printf("Sum = %d\n", fp(3, 4));   // call through fp
    return 0;
}
Output:
Sum = 7

A function's name by itself is its address, so fp = add works directly. Then fp(3, 4) calls add through the pointer.

Callbacks: passing behaviour around

Because a function pointer is just a value, you can pass it into another function. The receiver "calls back" whatever you gave it — this is a callback.

C Language
#include <stdio.h>
int add(int a, int b) { return a + b; }
int mul(int a, int b) { return a * b; }

void compute(int x, int y, int (*op)(int, int)) {
    printf("Result = %d\n", op(x, y));   // call whatever was passed
}
int main() {
    compute(4, 5, add);   // Result = 9
    compute(4, 5, mul);   // Result = 20
    return 0;
}
Output:
Result = 9
Result = 20

The same compute function behaves differently depending on which function you hand it. That flexibility is the whole point.

An array of function pointers (menu)

Store several functions in an array and pick one by index — perfect for a menu without a big switch.

C Language
#include <stdio.h>
int add(int a, int b){ return a + b; }
int sub(int a, int b){ return a - b; }
int mul(int a, int b){ return a * b; }

int main() {
    int (*ops[3])(int, int) = { add, sub, mul };
    int choice = 2;   // 0=add, 1=sub, 2=mul
    printf("Answer = %d\n", ops[choice](6, 3));
    return 0;
}
Output:
Answer = 18

Choosing index 2 calls mul, giving 6 × 3 = 18. Change choice to run a different operation with no if-else at all.

Why function pointers matter

  • Callbacks — let libraries call your code (event handlers, comparison functions for sorting).
  • Menus / dispatch tables — pick a function by number instead of long switches.
  • Plugins — swap behaviour at run time without changing the caller.

Common mistakes

  • Forgetting the parentheses around *fp, changing the meaning entirely.
  • Making the pointer's signature not match the real function's parameters or return type.
  • Calling a function pointer that was never assigned (still NULL or garbage).
  • Confusing fp (the pointer) with fp() (calling through it).
🏋️ Practice

Build a tiny calculator: an array of four function pointers for +, -, ×, ÷, and let the user pick an operation by number. Call the chosen function on two inputs and print the result.

Summary

  • A function pointer stores the address of a function, so you can call it indirectly.
  • Declare it as returnType (*name)(paramTypes) — parentheses around *name are required.
  • Assign it a function by name, then call with name(args).
  • Passing one into another function creates a callback.
  • An array of function pointers makes clean menus and dispatch tables.

Frequently Asked Questions

What is a function pointer in C?
A function pointer is a pointer that stores the address of a function instead of the address of a variable. Just as a normal pointer lets you reach a value, a function pointer lets you call a function through it — and even decide at run time which function to call.
How do you declare a function pointer in C?
You write the return type, then the pointer name in parentheses with a *, then the parameter types. For example int (*fp)(int, int); declares fp as a pointer to a function that takes two ints and returns an int. The parentheses around *fp are essential.
What is a callback in C?
A callback is a function passed into another function as an argument, using a function pointer, so the receiving function can call it back. This lets you customise behaviour — for example passing different comparison functions to a sorting routine so it sorts in different ways.
Why do the parentheses matter in a function pointer declaration?
Because int *fp(int) means "a function returning int *", while int (*fp)(int) means "a pointer to a function returning int". The parentheses around *fp change the meaning completely, so leaving them out is a classic mistake.
What is an array of function pointers used for?
An array of function pointers lets you pick a function by index, which is perfect for menus and command tables. Instead of a long chain of if-else or switch, you store the functions in an array and call the one the user selected with a single indexed call.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.