Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A function pointer is a variable that stores the memory address of executable code (a function) rather than a data value. It allows a program to invoke a function indirectly by dereferencing the pointer, provided the pointer’s type signature strictly matches the target function’s return type and parameter list.

Syntax and Declaration

The declaration of a function pointer requires specifying the exact signature of the functions it can point to. The asterisk * and the pointer name must be enclosed in parentheses to override standard operator precedence; otherwise, the compiler interprets it as a function returning a pointer.
// return_type (*pointer_name)(parameter_type_1, parameter_type_2, ...);

int (*operationPtr)(int, int); 

Initialization and Assignment

A function pointer is initialized by assigning it the uninvoked name of a function. The address-of operator (&) is optional because a function name implicitly decays into a pointer to that function.
int add(int a, int b) {
    return a + b;
}

// Both initializations are strictly equivalent
int (*ptr1)(int, int) = add;
int (*ptr2)(int, int) = &add; 

Invocation

Invoking a function through a pointer can be done using implicit or explicit dereferencing. Both approaches yield identical machine code.
// Implicit dereference (Standard C++ style)
int result1 = ptr1(5, 3);

// Explicit dereference (Legacy C style)
int result2 = (*ptr1)(5, 3);

Type Aliasing

Because raw function pointer syntax can become difficult to parse—especially when used as return types or parameters—C++ provides typedef and the modern using keyword to create type aliases.
// Legacy typedef approach
typedef int (*OperationFunc)(int, int);

// Modern C++ type alias approach (Recommended)
using OperationFuncAlias = int (*)(int, int);

// Usage
OperationFuncAlias myPtr = add;

Pointers to Member Functions

In C++, standard function pointers cannot store the address of non-static member functions. This is because non-static member functions require an implicit this pointer to operate on an object instance. To point to a non-static member function, you must use the pointer-to-member syntax (Class::*).
class Calculator {
public:
    int multiply(int a, int b) { return a * b; }
};

// Declaration of a pointer to a member function of Calculator
int (Calculator::*memPtr)(int, int) = &Calculator::multiply;
Invoking a pointer to a member function requires an instantiated object of the class and the use of the pointer-to-member operators (.* for objects/references, or ->* for pointers).
Calculator calc;
Calculator* calcPtr = &calc;

// Invocation via object instance
int res1 = (calc.*memPtr)(4, 5);

// Invocation via object pointer
int res2 = (calcPtr->*memPtr)(4, 5);
Note: Static member functions do not have an implicit this pointer. Therefore, they decay into standard function pointers and do not require the pointer-to-member syntax.
Master C++ with Deep Grasping Methodology!Learn More