Passing Pointers to Functions In C++
Last Updated :
28 Nov, 2022
Prerequisites:
Passing Pointers to functions means declaring the function parameter as a pointer, at the function calling passing the address of the variable and that address will be stored by a parameter that is declared as a pointer.
To change the value of any variable in the function we have to pass the address of that variable in the function.
Example:
C++
// C++ program to change value of x
// by passing an argument to a function
// without a pointer
#include <iostream>
using namespace std;
void fun(int x) { x = 5; }
// Driver code
int main()
{
int x = 9;
cout << "value of x before calling fun: " << x << endl;
fun(x);
cout << "value of x after calling fun: " << x << endl;
return 0;
}
Outputvalue of x before calling fun: 9
value of x after calling fun: 9
In the above code, the address of the value of the element x does not get affected by the value change in the function as the address of both the variables are different.
Another case can be where we want to change its value in the function. In such cases we use pointers.
Example:
C++
// C++ program to change values of x
// by passing an argument to a function
// with a pointer
#include <iostream>
using namespace std;
void fun(int* ptr) { *ptr = 5; }
// Driver code
int main()
{
int x = 9;
cout << "value of x before calling fun: " << x << endl;
fun(&x);
cout << "value of x after calling fun: " << x << endl;
return 0;
}
Outputvalue of x before calling fun: 9
value of x after calling fun: 5
In the above code, the actual value of x is passed as we are passing the address which is stored by a pointer ptr.
Example:
C++
// C++ program to swap two values
// without passing pointer to
// swap function.
#include <iostream>
using namespace std;
void swap(int x, int y)
{
int temp = x;
x = y;
y = temp;
}
// Driver code
int main()
{
int a = 2, b = 5;
cout << "values of a and b before swapping: " << a
<< " " << b << endl;
swap(a, b);
cout << "values of a and b after swapping: " << a << " "
<< b << endl;
return 0;
}
Outputvalues of a and b before swapping: 2 5
values of a and b after swapping: 2 5
Example:
C++
// C++ program to swap two values
// without passing pointer to
// swap function.
#include <iostream>
using namespace std;
void swap(int* x, int* y)
{
int temp = *x;
*x = *y;
*y = temp;
}
// Driver code
int main()
{
int a = 2, b = 5;
cout << "values of a and b before swapping: " << a
<< " " << b << endl;
swap(&a, &b); // passing address of a and b
cout << "values of a and b after swapping: " << a << " "
<< b << endl;
return 0;
}
Outputvalues of a and b before swapping: 2 5
values of a and b after swapping: 5 2
Function Pointer on Array
An array is a type of Data structure with continuous memory blocks which can store data with a similar data type. Also, the name of the array represents its first memory block address. So, we can use this property to access the elements of the array.
To know more about it, refer to the article - Array in C++.
Example:
C++
// C++ program to display element of
// array by passing argument to a
// function with pointer.
#include <iostream>
using namespace std;
void display(int* ptr, int n)
{
for (int i = 0; i < n; ++i) {
cout << *(ptr + i) << " ";
}
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int n = sizeof(arr) / sizeof(int);
// ptr will store the address of first block of array
int* ptr = arr;
// passing argument to a function as pointer.
display(ptr, n);
return 0;
}
Similar Reads
Passing Pointers to Functions in Objective-C Pointers are a crucial aspect of any programming language and passing pointers to functions in Objective-C is no exception. In Objective-C, pointers are used to pass values between functions and to manipulate memory in a more flexible manner. Pointers can be passed as arguments to functions and can
3 min read
Passing Vector to a Function in C++ To perform operations on vector belonging to one function inside other function, we pass this vector to the function as arguments during the function call.C++ provides three methods to pass a vector to a function in C++. Let's look at each of them one by one.Table of ContentPass Vector by ValuePass
5 min read
Function Pointer in C++ Prerequisites: Pointers in C++Function in C++ Pointers are symbolic representations of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. Iterating over elements in arrays or other data structures is one of the main use of point
4 min read
Function Pointer in C In C, a function pointer is a type of pointer that stores the address of a function, allowing functions to be passed as arguments and invoked dynamically. It is useful in techniques such as callback functions, event-driven programs, and polymorphism (a concept where a function or operator behaves di
6 min read
Pass Array to Functions in C++ In C++, a collection of elements stored in contiguous memory locations and having the same data type is called an array. Passing arrays to functions is done to perform various operations on array elements without messing up with the main code. In C++, an array can be passed in a function using a poi
5 min read
Pass Array to Functions in C Passing an array to a function allows the function to directly access and modify the original array. In this article, we will learn how to pass arrays to functions in C.In C, arrays are always passed to function as pointers. They cannot be passed by value because of the array decay due to which, whe
3 min read