Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Passing Dynamic Array as an Argument of the Function | Function Arguments Specification
C++ Functions

bookPassing Dynamic Array as an Argument of the Function

Passing 1-dimensional array

You can pass a 1-dimensional dynamic array to a function by passing a pointer to the array along with the array size as a separate parameter. Since arrays decay into pointers when passed to functions, you can pass a pointer to the first element of the array. Here's how you can do it:

main.cpp

main.cpp

copy
12345678910111213141516171819202122232425262728
#include <iostream> // Function that takes a dynamic array and its size as parameters void process(int* arr, const int size) { // Access elements of the array using the pointer and the size for (int i = 0; i < size; ++i) std::cout << arr[i] << " "; std::cout << std::endl; } int main() { // Dynamic array allocation int size = 5; int* dynamic_array = new int[size]; // Initializing the dynamic array for (int i = 0; i < size; ++i) dynamic_array[i] = i * 2; // Passing the dynamic array to the function process(dynamic_array, size); // Deallocate the dynamic array to prevent memory leaks delete[] dynamic_array; }

The processArray() function takes a dynamic integer array (int*) and its size as parameters. In main(), a dynamic array of size 5 is created, initialized, and passed to the function using its name and size.

Passing 2-dimensional array

When dealing with a dynamic 2-dimensional array (an array of pointers where each pointer points to an array of elements), you can pass it as a pointer to a pointer along with the dimensions to a function.

main.cpp

main.cpp

copy
1234567891011121314151617181920212223242526272829303132333435
#include <iostream> // Function that takes a dynamic 2D array and its size as parameters void process(int** arr, int rows, int cols) { for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) arr[i][j] = i * cols + j; // Fill array with values for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) std::cout << arr[i][j] << " "; std::cout << std::endl; } } int main() { int rows = 3; int cols = 4; // Dynamic 2D array allocation int** dynamicArray = new int*[rows]; for (int i = 0; i < rows; i++) dynamicArray[i] = new int[cols]; // Pass the 2D array to the function process(dynamicArray, rows, cols); // Deallocate the dynamic 2D array for (int i = 0; i < rows; i++) delete[] dynamicArray[i]; delete[] dynamicArray; }

The process() function takes a dynamically allocated 2D array (int**) and its dimensions as parameters, then fills it with values. The array is passed to the function using its name.

question mark

Which is the correct way to declare a function that accepts a dynamic 1-dimensional integer array?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 5

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you show an example of how to define and call these functions?

What are the advantages of using dynamic arrays over static arrays in this context?

How do I properly free the memory allocated for these dynamic arrays?

Awesome!

Completion rate improved to 5

bookPassing Dynamic Array as an Argument of the Function

Swipe to show menu

Passing 1-dimensional array

You can pass a 1-dimensional dynamic array to a function by passing a pointer to the array along with the array size as a separate parameter. Since arrays decay into pointers when passed to functions, you can pass a pointer to the first element of the array. Here's how you can do it:

main.cpp

main.cpp

copy
12345678910111213141516171819202122232425262728
#include <iostream> // Function that takes a dynamic array and its size as parameters void process(int* arr, const int size) { // Access elements of the array using the pointer and the size for (int i = 0; i < size; ++i) std::cout << arr[i] << " "; std::cout << std::endl; } int main() { // Dynamic array allocation int size = 5; int* dynamic_array = new int[size]; // Initializing the dynamic array for (int i = 0; i < size; ++i) dynamic_array[i] = i * 2; // Passing the dynamic array to the function process(dynamic_array, size); // Deallocate the dynamic array to prevent memory leaks delete[] dynamic_array; }

The processArray() function takes a dynamic integer array (int*) and its size as parameters. In main(), a dynamic array of size 5 is created, initialized, and passed to the function using its name and size.

Passing 2-dimensional array

When dealing with a dynamic 2-dimensional array (an array of pointers where each pointer points to an array of elements), you can pass it as a pointer to a pointer along with the dimensions to a function.

main.cpp

main.cpp

copy
1234567891011121314151617181920212223242526272829303132333435
#include <iostream> // Function that takes a dynamic 2D array and its size as parameters void process(int** arr, int rows, int cols) { for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) arr[i][j] = i * cols + j; // Fill array with values for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) std::cout << arr[i][j] << " "; std::cout << std::endl; } } int main() { int rows = 3; int cols = 4; // Dynamic 2D array allocation int** dynamicArray = new int*[rows]; for (int i = 0; i < rows; i++) dynamicArray[i] = new int[cols]; // Pass the 2D array to the function process(dynamicArray, rows, cols); // Deallocate the dynamic 2D array for (int i = 0; i < rows; i++) delete[] dynamicArray[i]; delete[] dynamicArray; }

The process() function takes a dynamically allocated 2D array (int**) and its dimensions as parameters, then fills it with values. The array is passed to the function using its name.

question mark

Which is the correct way to declare a function that accepts a dynamic 1-dimensional integer array?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 5
some-alt