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.
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
.
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;}
Line 4: We define the Increment()
function that takes number
pointer to an integer as a parameter.
Line 5: We dereference the number
pointer and increment its value.
Line 10: We declare a variable num
and set it to 5
.
Line 11: We declare a pointer variable, ptr
, and point it to the num
variable.
Line 12: We print the num
variable.
Line 13: We call the Increment()
function by passing the ptr
pointer as an argument.
Line 14: We print the value of the num
variable 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()
).
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;}
Line 4: We define the Increment()
function that has two parameters—integer type number
pointer and size
variable.
Line 6: We loop to iterate through an array.
Line 8: We dereference pointer
to access each index of the array and increment the value by 1
.
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 ptr
to arr
.
Lines 17–20: We print arr
before calling the Increment()
function.
Line 13: We pass ptr
and size
to the Increment()
function.
Lines 24–27: We print arr
after calling the Increment()
function.
Note: The type of pointer variable should be same as the type of variable to be pointed.
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