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
course content

Course Content

C++ Functions

Passing Dynamic Array as an Argument of the FunctionPassing 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.cpp

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

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.

Everything was clear?

Section 2. Chapter 5
course content

Course Content

C++ Functions

Passing Dynamic Array as an Argument of the FunctionPassing 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.cpp

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

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.

Everything was clear?

Section 2. Chapter 5
some-alt