1. Function Pointers
Declaration of Function Pointers
// Suppose there is a function that returns int and accepts two int parametersint add(int a, int b) { return a + b;}// Declare a pointer to such a functionint (*functionPtr)(int, int);
Initializing Function Pointers
functionPtr = add; // Point the function pointer to the add function
int result = functionPtr(5, 3); // Call the add functionThis is equivalent to directly calling add(5, 3).
Types of Function Pointers
typedef int (*FuncPtr)(int, int);FuncPtr functionPtr = add;int result = functionPtr(5, 3);
Function Pointers as Parameters
void callFunction(FuncPtr ptr, int a, int b) { int result = ptr(a, b); // Can do some processing}callFunction(add, 5, 3); // Function pointer array
You can create an array of function pointers to store multiple function pointers:
void (*actions[])(int) = {add, subtract, multiply, divide}; // Pointer to overloaded functions
int overloaded(int a, int b);double overloaded(double a, double b);int (*ptr1)(int, int) = overloaded; // Correctdouble (*ptr2)(double, double) = overloaded; // Correct
Pointer to Member Functions
class MyClass {public: void memberFunction() { // ... }};// Pointer to member functionvoid (MyClass::*memberFunctionPtr)() = &MyClass::memberFunction; // Using member function pointer requires calling through an object:
MyClass obj;(obj.*memberFunctionPtr)(); // Call obj's memberFunction
Notes
-
Function pointers can only point to functions with the same signature.
-
When using pointers to member functions, be aware of the differences from regular function pointers.
-
Function pointers can be used to implement advanced programming techniques, such as callback functions, event listeners, etc.
Function pointers are a powerful feature in C++, providing a flexible way to reference and call functions.
2. Pointer Functions
Functions that Return Pointers
int* function() { int value = 42; return &value; // Returns the address of a local variable, which is dangerous because the local variable will be destroyed after the function returns}
Returning Pointers to Static Variables
int* function() { static int value = 42; return &value; // Returns the address of the static variable}
Returning Pointers to Dynamically Allocated Memory
int* function() { int* value = new int(42); // Dynamically allocate memory return value; // Return the address of dynamically allocated memory}
Returning Pointers to Arrays
int* function(int size) { static int array[size]; // VLA (Variable Length Array), C99 standard for (int i = 0; i < size; ++i) { array[i] = i; } return array; // Return pointer to the array}
Pointer parameters and pointer return values
int* function(int* ptr) { if (ptr) { return ptr; // Return the passed pointer } return nullptr; // If passed nullptr, return nullptr}
Notes
-
When returning the address of a local variable, ensure that the lifetime of that local variable is longer than the period of use of that address.
-
Dynamically allocated memory needs to be released.
-
When returning the address of static or global variables, ensure thread safety (if the program is multithreaded).
-
When returning pointers to arrays, ensure that the caller knows the size of the array to avoid out-of-bounds access.

Want to learn more
Quickly scan the code to follow