Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Returning Arrays from the Function | Function Return Values Specification
C++ Functions
course content

Conteúdo do Curso

C++ Functions

C++ Functions

1. Introduction
2. Function Arguments Specification
3. Function Return Values Specification
4. Some Advanced Topics

book
Returning Arrays from the Function

Just like simple data types, arrays can be returned as function results.

However, there is a crucial limitation: only dynamic arrays can be returned from functions. This restriction arises from the local scope of functions; static arrays created within a function are only accessible within that function.

If you attempt to return a locally created array, you'll encounter a limitation due to the function's scope (you will try to return an address of non-existing variables that will lead to an error).

Returning a dynamic array

Why can we return dynamic arrays?
Unlike local variables, which are deallocated when a function exits, dynamic memory allocated using new[] allows data to survive beyond the scope of the function where it was created. As a result, we can access variables created inside the function using their addresses.
To return a dynamic array, we have to use the following type specifiers in the function signature:

  • dataType* for 1D array.
  • dataType** for 2D array.

Returning 1D array

cpp

main

copy
12345678910111213141516171819
#include <iostream> // Function to create and return a dynamic 1D array int* createArray(const int size) { int* arr = new int[size]; for (int i = 0; i < size; ++i) arr[i] = i + 1; // Example initialization return arr; } int main() { int size = 5; int* myArray = createArray(size); // Don't forget to delete the dynamically allocated memory delete[] myArray; }

The function dynamically allocates memory for an integer array of the specified size. It initializes the array elements with values from 1 to the size of the array. The function returns a pointer on the first element of the dynamically allocated integer array that can now be used in the main() block.

Returning 2D array

cpp

main

copy
1234567891011121314151617181920212223242526272829303132333435
#include <iostream> // Function to create and return a dynamic 2D array int** createArray(const int rows, const int cols) { int** arr = new int*[rows]; for (int i = 0; i < rows; ++i) { arr[i] = new int[cols]; for (int j = 0; j < cols; ++j) { arr[i][j] = i * cols + j + 1; // Example initialization } } return arr; } int main() { int rows = 3; int cols = 2; int** myArray = createArray(rows, cols); // Use the returned 2D array for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { std::cout << myArray[i][j] << " "; } std::cout << std::endl; } // Don't forget to delete the dynamically allocated memory for (int i = 0; i < rows; ++i) { delete[] myArray[i]; } delete[] myArray; return 0; }

The principle is the same as returning a 1D array: the only difference is that we have to return a pointer at the array of pointers using the int** type specifier to return the 2D array.

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 2
We're sorry to hear that something went wrong. What happened?
some-alt