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

bookPassing Static Array as an Argument of the Function

Pass a 1-Dimensional Array as an Argument

To pass a 1-dimensional array to a function, place [] after the parameter name in the function signature.

main.cpp

main.cpp

copy
123456789101112131415161718192021
#include <iostream> // Function to process a 1-dimensional static array void process(int arr[], const int size) { for (int i = 0; i < size; ++i) std::cout << arr[i] << " "; // Print each element of the array std::cout << std::endl; } int main() { const int SIZE = 5; // Initialize a 1-dimensional static array int oneDimArray[SIZE] = {1, 2, 3, 4, 5}; // Call the function to process the array std::cout << "Original Array: "; process(oneDimArray, SIZE); }

Pass a 2-Dimensional Array as an Argument

Passing a 2D array to a function works similarly to passing a 1D array — you use [][] after the parameter name.

However, there is an important difference: in C++, you cannot declare a function parameter as datatype arrayName[][] without specifying at least one dimension. You must define the number of columns (or one dimension) so the compiler can correctly calculate memory offsets when accessing array elements.

main.cpp

main.cpp

copy
1234567891011121314151617181920212223
#include <iostream> // Function to print a 2D array with a fixed number of columns void process(int matrix[][3], const int rows) { // Loop through rows for (int i = 0; i < rows; ++i) { for (int j = 0; j < 3; ++j) // Loop through columns std::cout << matrix[i][j] << " "; // Print each element std::cout << std::endl; // Move to the next line } } int main() { const int ROWS = 2; int arr[ROWS][3] = {{1, 2, 3}, {4, 5, 6}}; std::cout << "Original Matrix:" << std::endl; process(arr, ROWS); // Pass array and row count to the function }

Since we need to specify at least one of the array dimensions (it doesn’t matter which), the signature of this function can also look like this:

question mark

Which function signature correctly accepts a 2D static array as a parameter?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 4

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Awesome!

Completion rate improved to 5

bookPassing Static Array as an Argument of the Function

Scorri per mostrare il menu

Pass a 1-Dimensional Array as an Argument

To pass a 1-dimensional array to a function, place [] after the parameter name in the function signature.

main.cpp

main.cpp

copy
123456789101112131415161718192021
#include <iostream> // Function to process a 1-dimensional static array void process(int arr[], const int size) { for (int i = 0; i < size; ++i) std::cout << arr[i] << " "; // Print each element of the array std::cout << std::endl; } int main() { const int SIZE = 5; // Initialize a 1-dimensional static array int oneDimArray[SIZE] = {1, 2, 3, 4, 5}; // Call the function to process the array std::cout << "Original Array: "; process(oneDimArray, SIZE); }

Pass a 2-Dimensional Array as an Argument

Passing a 2D array to a function works similarly to passing a 1D array — you use [][] after the parameter name.

However, there is an important difference: in C++, you cannot declare a function parameter as datatype arrayName[][] without specifying at least one dimension. You must define the number of columns (or one dimension) so the compiler can correctly calculate memory offsets when accessing array elements.

main.cpp

main.cpp

copy
1234567891011121314151617181920212223
#include <iostream> // Function to print a 2D array with a fixed number of columns void process(int matrix[][3], const int rows) { // Loop through rows for (int i = 0; i < rows; ++i) { for (int j = 0; j < 3; ++j) // Loop through columns std::cout << matrix[i][j] << " "; // Print each element std::cout << std::endl; // Move to the next line } } int main() { const int ROWS = 2; int arr[ROWS][3] = {{1, 2, 3}, {4, 5, 6}}; std::cout << "Original Matrix:" << std::endl; process(arr, ROWS); // Pass array and row count to the function }

Since we need to specify at least one of the array dimensions (it doesn’t matter which), the signature of this function can also look like this:

question mark

Which function signature correctly accepts a 2D static array as a parameter?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 4
some-alt