Return Pointer to Array in C++

  1. Use int var[n] Notation to Pass the Array Argument to Function and Then Return in C++
  2. Use int* var Notation to Pass the Array Argument to Function and Then Return in C++

This article will explain several methods of returning a pointer to an array in a function in C++.

Use int var[n] Notation to Pass the Array Argument to Function and Then Return in C++

Since the function needs to return the pointer value, we will assume that the array is fixed-length. Alternatively, if we have to pass a dynamic array - std::vector to a function, it’s better to use references.

The next example demonstrates the subtructArray function that subtracts each value in the array given the subtrahend value. The array is declared as a raw C-style array, which is mostly useful for operating with pointers. The array is passed with the int arr[] notation of the parameter, but it is converted underneath by the compiler as the pointer to the array and we can treat it as such in the function body. Finally, it returns the pointer value directly using the variable name without taking its address with the & operator.

The subtracted array elements are outputted to the console, and after outputting the last element, there is the cout statement that includes \b\b in the string literal. This escape sequence means the backspace key behavior is emulated, thus deleting the last two characters in the console output.

#include <iostream>
#include <array>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::array;

int *subtructArray(int arr[], int size, int subtrahend)
{
    for (int i = 0; i < size; ++i) {
        arr[i] -= subtrahend;
    }
    return arr;
}

constexpr int SIZE = 10;

int main(){
    int c_array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int num = 15;

    int *ptr = subtructArray(c_array, SIZE, num);

    cout << "array1 = [ ";
    for (int i = 0; i < SIZE; ++i) {
        cout << ptr[i] << ", ";
    }
    cout << "\b\b ]" << endl;

    return EXIT_SUCCESS;
}

Output:

array1 = [ -14, -13, -12, -11, -10, -9, -8, -7, -6, -5 ]

Use int* var Notation to Pass the Array Argument to Function and Then Return in C++

Another method for passing the fixed-sized array is to declare a function parameter with int* var notation and return the pointer value after the function body is done processing it. Note that the next sample code uses the std::array container and calls the data() method to retrieve the pointer where the array elements are stored. The return statement takes the variable name as in the previous method.

#include <iostream>
#include <array>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::array;

int *subtructArray(int *arr, int size, int subtrahend)
{
    for (int i = 0; i < size; ++i) {
        arr[i] -= subtrahend;
    }
    return arr;
}

constexpr int SIZE = 10;

int main(){
    array<int, SIZE> arr1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int num = 15;

    int *ptr2 = subtructArray(arr1.data(), arr1.size(), num);

    cout << "array2 = [ ";
    for (int i = 0; i < SIZE; ++i) {
        cout << ptr2[i] << ", ";
    }
    cout << "\b\b ]" << endl;

    return EXIT_SUCCESS;
}

Output:

array1 = [ -14, -13, -12, -11, -10, -9, -8, -7, -6, -5 ]

Related Article - C++ Array

  • Return 2D Array From Function in C++
  • Reverse Array in C++