How to pass a pointer to a function in C++
A pointer is a variable that stores the address of an object stored in memory. In this answer, we'll see how to pass a pointer to a function in C++. We can pass a pointer to the function in the following ways:
Passing variable as a pointer
Passing array as a pointer
Let's see the steps to pass a pointer to a function as a parameter.
Steps to pass a pointer to a function
To pass a variable as a pointer to a function, we can follow these three simple steps:
Define a function that takes a pointer as a parameter. For example:
void function(int *pointer) {// function code goes here}
Declare a pointer variable and point it to the variable or an array we want to pass to the function like this:
// For variableint value = 5;int *pointer = &value;// For arrayint arr[] = {1, 2, 3, 4, 5};int *pointer = arr;
This will create a pointer variable named pointer that contains the address of the value variable.
Call the function by passing the pointer as an argument, as shown below:
function(pointer);
This will pass the pointer variable, pointer, to the function() function.
Note: We can access the value pointed by the pointer variable by using the
*operator like*pointer.
Passing variable as a pointer
Let's see the code to pass the pointer pointing to the variable as a parameter to the function:
#include <iostream>using namespace std;void Increment(int *number){*number = *number + 1;}int main() {int num = 5;int *ptr = #cout << "Number before incrementing is " << num << endl;Increment(ptr);cout << "Number after incrementing is " << num << endl;return 0;}
Explanation
Line 4: We define the
Increment()function that takesnumberpointer to an integer as a parameter.Line 5: We dereference the
numberpointer and increment its value.Line 10: We declare a variable
numand set it to5.Line 11: We declare a pointer variable,
ptr, and point it to thenumvariable.Line 12: We print the
numvariable.Line 13: We call the
Increment()function by passing theptrpointer as an argument.Line 14: We print the value of the
numvariable after calling the function.
Remember: If we pass the variable to the function instead of passing a pointer (address) to the variable, then the changes to the variable inside the function will not be reflected outside of the function (e.g.
main()).
Passing an array as a pointer
Let's see the code to pass the pointer pointing to an array as a parameter to the function:
#include <iostream>using namespace std;void Increment(int *pointer, int size){for(int i = 0; i < size; i++){(*(pointer + i))++; // pointer[i]++;}}int main() {int arr[] = {2, 3, 5, 7, 11};int size = 5;int *ptr = arr;cout << "Array before incrementing is: ";for (int i = 0; i < size; i++)cout << arr[i] << " ";cout << endl;Increment(ptr, size);cout << "Array after incrementing is: ";for (int i = 0; i < size; i++)cout << arr[i] << " ";cout << endl;return 0;}
Explanation
Line 4: We define the
Increment()function that has two parameters—integer typenumberpointer andsizevariable.Line 6: We loop to iterate through an array.
Line 8: We dereference
pointerto access each index of the array and increment the value by1.Line 13: We declare an array,
arr, and initialize it.Line 14: We store the size of an array in a variable
size.Line 15: We create a pointer
ptrtoarr.Lines 17–20: We print
arrbefore calling theIncrement()function.Line 13: We pass
ptrandsizeto theIncrement()function.Lines 24–27: We print
arrafter calling theIncrement()function.
Note: The type of pointer variable should be same as the type of variable to be pointed.
Conclusion
Conclusively, to pass a pointer to a function in C++, we define a function that takes a pointer as a parameter, create a pointer variable that points to the value we want to pass, and pass it to the function. We can use the pointer to access or modify the original value inside the function.
Free Resources