Function Pointer Arrays

Arrays of Function Pointers in C++

Learn how to declare, initialize, and call an array of function pointers in C++, with a full working example, a type alias, and a std::array version.

On this page
  1. What an array of function pointers is
  2. Function pointer basics
  3. Declaring the array
  4. A complete example
  5. Shortening the type with an alias
  6. Using std::array
  7. Lambdas in the array
  8. Calling through the array safely
  9. Where arrays of function pointers help
  10. Pointers to member functions
  11. Common mistakes
  12. Summary

What an array of function pointers is

An array of function pointers is an array in which each element stores the address of a function. Every function in the array needs the same signature, meaning the same return type and the same parameter types. Once the array is set up, you choose a function by its index and call it.

The declaration is the part most people find hard to read. This one declares an array of three pointers, each pointing to a function that takes two int values and returns an int:

int (*ops[3])(int, int);

The parentheses around *ops[3] are required. Without them, the name becomes a function that returns an array, which C++ does not allow, so the code does not compile.

To read the declaration, start at the name and work outward. ops[3] says ops is an array of three elements. The * says each element is a pointer. The trailing (int, int) says each pointer refers to a function taking two int values, and the leading int is the return type.

Function pointer basics

A function pointer stores the address of a function. Here is a single one that points to add:

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

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

A function name used as a value turns into a pointer to that function, so add works here just as &add does. Calling through the pointer looks like a normal call. op(2, 3) returns 5.

Declaring the array

The same pattern works for several functions at once. Here are three small functions with the same signature:

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; }

int (*ops[])(int, int) = { add, subtract, multiply };

Leaving the size empty lets the compiler count the initializers, so ops holds three pointers. You can also give the size explicitly, such as ops[3], but the initializer list cannot have more values than that size.

A complete example

#include <cstddef>
#include <iostream>
#include <iterator>

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; }

int main() {
    int (*ops[])(int, int) = { add, subtract, multiply };

    for (std::size_t i = 0; i < std::size(ops); ++i) {
        std::cout << "op " << i << ": " << (*ops[i])(7, 3) << '\n';
    }
}

Output:

op 0: 10
op 1: 4
op 2: 21

In (*ops[i])(7, 3), the index picks an element, which is a pointer. The * dereferences that pointer to get the function, and (7, 3) calls it. The * is optional when calling, but writing it out makes it clear that an element is being called. std::size needs C++17 or later.

Shortening the type with an alias

Long declarations are hard to scan, so many programs give the pointer type a name with using:

using BinaryOp = int (*)(int, int);

BinaryOp ops[] = { add, subtract, multiply };

Now the array reads as a list of BinaryOp values. The same alias can be used for function parameters and return types, which keeps those signatures short.

Using std::array

std::array keeps its size with the data, and its at() member checks the index. Here is the same idea with the alias and a range-based loop:

#include <array>
#include <iostream>
#include <stdexcept>

using BinaryOp = int (*)(int, int);

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; }

int main() {
    std::array<BinaryOp, 3> ops = { add, subtract, multiply };

    for (BinaryOp op : ops) {
        std::cout << op(7, 3) << '\n';
    }

    try {
        std::cout << ops.at(5)(7, 3) << '\n';
    } catch (const std::out_of_range&) {
        std::cout << "No function at index 5\n";
    }
}

Output:

10
4
21
No function at index 5

at() throws std::out_of_range when the index is outside the array, so the bad call never happens. A C-style array has no such check. Reading or calling past its end is undefined behavior. If you want the length to travel with the array, std::array is the usual choice. For more on that type, see c std::array.

Lambdas in the array

A lambda with an empty capture list can go into the array. The compiler converts it to a plain function pointer with a matching signature:

BinaryOp ops[] = {
    add,
    [] (int a, int b) { return a % b; }
};

A lambda that captures a local variable cannot be converted this way, because a plain function pointer has no place to store the captured value. For that case, look at c std::function, a wrapper that can hold lambdas with captures. If the lambda syntax itself is new to you, c lambda function explains it step by step.

Calling through the array safely

Three checks prevent most bad calls:

  • Null entries. If an array is only partly initialized, the remaining elements are null pointers. Calling a null function pointer is undefined behavior, so check first:
BinaryOp op = ops[index];
if (op != nullptr) {
    std::cout << op(7, 3) << '\n';
}
  • Index range. std::array::at() checks the index. A C-style array does not, so the code must keep the index between 0 and the size minus 1.
  • Exact signatures. A function with a different return type or parameter list does not convert to BinaryOp. Trying to store double divide(double, double) in the array produces a compile error, which is useful feedback.

Where arrays of function pointers help

A common use is a lookup table, where a value picks a function by index. Menus, command codes, and small state machines often follow this pattern. Storing the functions once in an array can replace a long if-else chain, and each operation lives in one place, which makes it easy to add a new one.

With a std::array, a menu choice can call the matching function directly:

int choice = 2;
std::cout << ops.at(choice)(7, 3) << '\n';

A switch statement can do the same job, and it is often easier to read when there are only a few cases.

Pointers to member functions

A pointer to a member function has different syntax and must be called through an object. It is a separate topic, so this article sticks to ordinary free functions.

Common mistakes

  • Missing parentheses. Without them, the name becomes a function that returns an array, which does not compile.
  • Mismatched signatures. Functions that differ in return type or parameters cannot go into the array.
  • Unchecked indexes and null entries. Calling past the end or through a null pointer leads to undefined behavior.
  • Assuming the size is stored. A C-style array has no size() member, so the count has to come from your code or from sizeof in the scope where the array is declared.

Summary

An array of function pointers is a fixed table of functions that share one signature. Declare it with parentheses around the name, use a type alias to keep it readable, and check indexes and null entries before each call.

Keep exploring

Guides from other topics

All topics →

std::list in C++

std::list is the C++ standard library's doubly linked list. Learn to create one, add and remove elements, loop through it, and when to pick it over a vector.

std::list 5 min read