Passing Pointers to Functions In C++

Last Updated : 18 Jun, 2026

Passing a pointer to a function means passing the address of a variable instead of its value. This allows the function to work directly with the original data without creating a copy.

  • Allows changes made inside the function to be reflected in the original variable.
  • Improves performance by avoiding unnecessary copies of large objects.
  • Commonly used with arrays, dynamic memory allocation, and pointer-based data structures.

Prerequisites

Example: The following example passes the address of a variable to a function so that its original value can be modified.

C++
#include <iostream>
using namespace std;

void update(int* ptr) {
    *ptr = 20;
}

int main() {
    int x = 10;

    update(&x);

    cout << x;

    return 0;
}

Output
20

Explanation: The function receives the address of x. By dereferencing the pointer (*ptr), it directly updates the original variable.

Syntax

return_type function_name(data_type* ptr);

Here,

  • return_type specifies the return type of the function.
  • data_type specifies the type of data the pointer points to.
  • * indicates that the parameter is a pointer.
  • ptr stores the address passed to the function.

Passing by Value vs Passing a Pointer

Functions can receive arguments either by value or by pointer. When passed by value, the function works on a copy of the variable. When passed by pointer, the function works on the original variable through its memory address.

Example: Passing by Value

C++
#include <iostream>
using namespace std;

void fun(int x)
{
    x = 5;
}

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;
}

Output
Value of x before calling fun: 9
Value of x after calling fun: 9

Explanation: The function receives a copy of x. Any modifications are made only to this copy, so the original variable remains unchanged.

Example: Passing a Pointer

C++
#include <iostream>
using namespace std;

void fun(int* ptr)
{
    *ptr = 5;
}

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;
}

Output
Value of x before calling fun: 9
Value of x after calling fun: 5

Explanation: The function receives the address of x. After dereferencing the pointer, it modifies the original variable, so the updated value is visible in main().

Swapping Variables Using Pointers

Passing pointers is especially useful when a function needs to modify multiple variables. A common example is swapping two values.

Example: Swapping Without Pointers

C++
#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;
}

Output
values of a and b before swapping: 2 5
values of a and b after swapping: 2 5

Explanation: Since the function receives copies of a and b, swapping them affects only the local copies. The original variables remain unchanged.

Example: Swapping Using Pointers

C++
#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;
}

Output
values of a and b before swapping: 2 5
values of a and b after swapping: 5 2

Explanation: The function receives the addresses of a and b. By dereferencing the pointers, it swaps the original values, so the changes are reflected after the function call.

Passing Arrays to Functions Using Pointers

The name of an array represents the address of its first element. Therefore, an array can be passed to a function as a pointer, allowing efficient access to its elements.

Example: Passing an Array to a Function

C++
#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;
}

Output
1 2 3 4 5 

Explanation: The array name arr is passed as a pointer to its first element. The function uses pointer arithmetic (*(ptr + i)) to access and print each array element.

Advantages of Passing Pointers to Functions

Passing pointers allows functions to work directly with the original data while avoiding unnecessary copies.

  • Allows functions to modify the original variables.
  • Improves performance by avoiding copies of large objects.
  • Efficient for passing arrays and dynamically allocated memory.
  • Useful for implementing dynamic data structures and output parameters.

Applications of Passing Pointers to Functions

Passing pointers is commonly used in the following situations:

  • Modifying variables inside a function.
  • Passing arrays efficiently without copying elements.
  • Working with dynamically allocated memory.
  • Implementing linked lists, trees, graphs, and other pointer-based data structures.
  • Returning multiple values from a function through pointer parameters.
Comment