Passing 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
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
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:
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Awesome!
Completion rate improved to 5.26
Passing 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
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
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:
Дякуємо за ваш відгук!