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

Зміст курсу

C++ Functions

C++ Functions

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

Passing Dynamic Array as an Argument of the Function

In C++, a dynamic array is an array whose size can be changed during runtime. Unlike static arrays, where the size is fixed at compile time, dynamic arrays allow you to allocate memory for the array at runtime and resize it as needed.

Let's consider how to pass 1-dimensional and 2-dimensional dynamic arrays as function arguments.

Passing 1-dimensional array

In C++, 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:

cpp

main

123456789101112131415161718192021222324252627
#include <iostream> // Function that takes a dynamic array and its size as parameters void processArray(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] << " "; // cout instead of std::cout std::cout << std::endl; // endl instead of std::endl } int main() { // Dynamic array allocation int size = 5; int* dynamicArray = new int[size]; // Initializing the dynamic array for (int i = 0; i < size; ++i) dynamicArray[i] = i * 2; // Passing the dynamic array to the function processArray(dynamicArray, size); // Deallocate the dynamic array to prevent memory leaks delete[] dynamicArray; }

In this example, processArray() is a function that takes a dynamic integer array (using int* type specifier) and its size as parameters. A dynamic array of size 5 is created and initialized in the main() function. The processArray() function is then called, passing the dynamic array using its name and its size as arguments.

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. Here's how you can do it:

cpp

main

1234567891011121314151617181920212223242526272829303132333435
#include <iostream> // Function to fill a dynamic 2D array with values void fillDynamic2DArray(int** arr, const int rows, const int cols) { // Fill the array with values (for demonstration purposes) for (int i = 0; i < rows; ++i) for (int j = 0; j < cols; ++j) arr[i][j] = i * cols + j; // You can modify this line to fill the array as desired // Print the filled array std::cout << "Filled Dynamic 2D Array:" << std::endl; 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; // Allocate a dynamic 2D array in the main function int** dynamic2DArray = new int*[rows]; for (int i = 0; i < rows; ++i) dynamic2DArray[i] = new int[cols]; // Call the function to fill and print the dynamic 2D array fillDynamic2DArray(dynamic2DArray, rows, cols); // Deallocate the dynamic 2D array for (int i = 0; i < rows; ++i) delete[] dynamic2DArray[i]; delete[] dynamic2DArray; }

In this example, the fillDynamic2DArray() function takes a dynamically allocated 2D array as input (using int** type specifier) along with its dimensions and fills it with values. We use the name of the array to pass it as an argument.

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

Секція 2. Розділ 5
We're sorry to hear that something went wrong. What happened?
some-alt