Pass Argument by Reference vs by Pointer in C++

  1. Use &variable Notation to Pass Function Arguments by Reference in C++
  2. Use *variable Notation to Pass Function Arguments by Reference in C++
  3. Use (&array_variable)[x][y] Notation to Pass 2D Array by Reference in C++

This article will explain several methods of passing an argument by the reference vs pass by the pointer in C++.

Use &variable Notation to Pass Function Arguments by Reference in C++

Passing arguments is the most common feature of functions to provide a flexible interface for data exchange between different code blocks. Each time function is invoked, its parameters are created and initialized by the arguments that were passed. Generally, two methods are differentiated for argument passing: passed by value and passed by reference, the latter of which causes the parameter to be an alias for the corresponding argument. We demonstrate a basic integer swap function in the following example, where arguments are passed by references and int &var notation is used. When the swapIntegers function returns, the int objects in the main function scope are modified.

#include <iostream>

using std::cout;
using std::endl;

void swapIntegers(int& x, int& y)
{
    int z = x;
    x = y;
    y = z;
}

int main()
{
    int a = 45, b = 35;
    cout << "a = " << a << " b = " << b << "\n";

    cout << "Swapping a and b ...\n";
    swapIntegers(a, b);

    cout << "a = " << a << " b = " << b << "\n";

    return EXIT_SUCCESS;
}

Output:

a = 45 b = 35
Swapping a and b ...
a = 35 b = 45

Use *variable Notation to Pass Function Arguments by Reference in C++

Similar behavior to the previous example can be implemented using pointers. Note that a pointer is an address of the object, and it can be dereferenced with the * operator to access the object value. Passing the arguments using the pointer means that we can access the given objects from the function scope and modify their values so that the state is preserved after the return. Notice that, to pass pointers to the function, they need to be accessed using the address-of (&) operator.

#include <iostream>
#include <iomanip>

using std::cout;
using std::endl;
using std::setw;

void swapIntegers2(int* x, int* y)
{
    int z = *x;
    *x = *y;
    *y = z;
}

int main()
{
    int a = 45, b = 35;
    cout << "a = " << a << " b = " << b << "\n";

    cout << "Swapping a and b ...\n";
    swapIntegers2(&a, &b);

    cout << "a = " << a << " b = " << b << "\n";

    return EXIT_SUCCESS;
}

Output:

a = 45 b = 35
Swapping a and b ...
a = 35 b = 45

Use (&array_variable)[x][y] Notation to Pass 2D Array by Reference in C++

Sometimes it can be handy to pass two-dimensional C-style array reference to the function, but the notation is slightly nonintuitive and could lead to erroneous results. If we have an array of integers with the arbitrary SIZE dimensions, it can be referenced from the function parameter with the following notation - int (&arr)[SIZE][SIZE]. Mind that, missing the parenthesis () will be interpreted as the array of references to int objects and result in compilation error.

#include <iostream>
#include <iomanip>

using std::cout;
using std::endl;
using std::setw;

void MultiplyArrayByTwoV2(int (&arr)[SIZE][SIZE])
{
    for (auto & i : arr) {
        for (int & j : i)
            j *= 2;
    }
}

void printArray(int (&arr)[SIZE][SIZE]) {
    for (auto & i : arr) {
        cout << " [ ";
        for (int j : i) {
            cout << setw(2) << j << ", ";
        }
        cout << "]" << endl;
    }
}

constexpr int SIZE = 4;

int main()
{
    int array_2d[SIZE][SIZE] = {{ 1, 2, 3, 4 },
                               { 5, 6, 7, 8 },
                               { 9, 10, 11, 12 },
                               { 13, 14, 15, 16 }};

    MultiplyArrayByTwoV2(array_2d);
    printArray(array_2d);

    return EXIT_SUCCESS;
}

Output:

[  2,  4,  6,  8, ]
[ 10, 12, 14, 16, ]
[ 18, 20, 22, 24, ]
[ 26, 28, 30, 32, ]
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - C++ Function

  • Return an Array From a Function in C++