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

bookReturning Arrays from the Function

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

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.
main.cpp

main.cpp

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.

main.cpp

main.cpp

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.

question mark

Which statement about returning arrays from functions in is correct?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you show an example of how to return a dynamic array from a function?

What happens if I try to return a static array from a function?

How do I properly deallocate the memory for a dynamic array after using it?

Awesome!

Completion rate improved to 5

bookReturning Arrays from the Function

Свайпніть щоб показати меню

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

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.
main.cpp

main.cpp

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.

main.cpp

main.cpp

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.

question mark

Which statement about returning arrays from functions in is correct?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 2
some-alt