C++ Pointers & Functions
In this page:
Passing Pointers to Functions
Passing pointers to functions lets the function reach into the caller's memory and modify the original variables directly, a technique known as passing by pointer. This is how functions simulate 'output parameters' in a language that otherwise passes arguments by value.
Example: Passing Pointers to Functions
#include <iostream>
void setValue(int *ptr, int value) {
*ptr = value;
}
int main() {
int num = 0;
setValue(&num, 42);
std::cout << num << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Returning Pointers from Functions
A function can return a pointer, but you should never return a pointer to a local variable created inside that function, since local variables live on the stack and are destroyed the moment the function returns. Only return pointers to static memory, heap-allocated memory, or memory owned by the caller.
Example: Returning Pointers from Functions
#include <iostream>
int *makeHeapValue(int value) {
int *ptr = new int(value); // never return a pointer to a local variable
return ptr;
}
int main() {
int *result = makeHeapValue(10);
std::cout << *result << std::endl;
delete result;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Constant Pointers in Functions
You can protect a function's parameters from modification by declaring the pointer as const, telling both the compiler and future readers that the pointed-to data is read-only inside that function. This catches accidental writes at compile time instead of at runtime.
Example: Constant Pointers in Functions
#include <iostream>
void printValue(const int *ptr) {
std::cout << *ptr << std::endl;
// *ptr = 5; // compile error: cannot modify through a const pointer
}
int main() {
int num = 7;
printValue(&num);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Function Pointers
Because functions occupy addresses in memory just like variables, C++ lets you declare pointers to functions and call them indirectly at runtime, choosing which function to invoke based on program logic rather than hardcoding the call. This is the low-level mechanism behind dynamic dispatch.
Example: Function Pointers
#include <iostream>
int square(int x) {
return x * x;
}
int main() {
int (*funcPtr)(int) = square;
std::cout << funcPtr(5) << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Callback Functions with Function Pointers
You can pass function pointers as arguments to other functions, letting a general-purpose function (like a sorter or event handler) execute custom behavior supplied by the caller. This callback pattern predates std::function and lambdas but still appears throughout C-style C++ APIs.
Example: Callback Functions with Function Pointers
#include <iostream>
void runTwice(void (*callback)()) {
callback();
callback();
}
void sayHi() {
std::cout << "Hi!" << std::endl;
}
int main() {
runTwice(sayHi);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: