Passing a 2D Array Parameter to Functions in C++
In C++, arrays are always passed as pointers. The same goes for the two-dimensional arrays. There exist many ways of passing a 2D array to functions in C++.
Method 1: Passing 2D Array with Rows and Columns
return_type name (array_type array_name[rows][coloumns], int rows, int col)
Example
The below program demonstrates passing a 2D Array with a known number of rows and columns
C++
// C++ program to demonstrate passing of 2D Array with known // number of rows and columns #include <iostream> using namespace std; // function to print the array void printArr( int arr[3][2], int n, int m) { // iterating through 2D array and printing elements for ( int i = 0; i < n; i++) { for ( int j = 0; j < m; j++) { cout << arr[i][j] << " " ; } cout << endl; } } int main() { const int n = 3, m = 2; int arr[n][m] = { { 10, 15 }, { 20, 25 }, { 30, 35 } }; // calling print function by passing array with row and // column size printArr(arr, n, m); return 0; } |
10 15 20 25 30 35
Note: We need to pass the dimensions of the array manually as the array loose the information about its dimensions due to array decay.
Method 2: Passing 2D Array with Rows and Columns Declared Globally.
return_type name (array_type array_name[rows][coloumns])
Example
The below program demonstrates passing a 2D Array with a known number of rows and columns that are declared globally.
C++
// C++ program to demonstrate passing of 2D Array When the // number of rows and columns of a 2D array are known and // declared globally #include <iostream> using namespace std; // declaring global no of rows and cols const int n = 3, m = 2; // function to print an array void printArr( int arr[n][m]) { for ( int i = 0; i < n; i++) { for ( int j = 0; j < m; j++) { cout << arr[i][j] << " " ; } cout << endl; } } int main() { int arr[n][m] = { { 10, 15 }, { 20, 25 }, { 30, 35 } }; // calling print function printArr(arr); return 0; } |
10 15 20 25 30 35
Method 3: Passing 2D Array with only the Number of Columns
return_type name (array_type array_name[][coloumns])
or
return_type name (array_type (*array_name)[coloumns])
Example
The below program demonstrates passing a 2D Array with a known number of columns only.
C++
// C++ program to demonstrates the passing of 2D array When // only the number of the column of a 2D array is Known. #include <iostream> using namespace std; // function to print an array void printArr( int arr[][2], int n, int m) { for ( int i = 0; i < n; i++) { for ( int j = 0; j < m; j++) { cout << arr[i][j] << " " ; } cout << endl; } } int main() { int m = 2; int arr[][2] = { { 100, 105 }, { 120, 125 }, { 130, 135 } }; // finding row size int n = sizeof arr / sizeof arr[0]; // calling print function printArr(arr, n, m); return 0; } |
100 105 120 125 130 135
Note: We always need to mention the size of the column as it is mandatory for a 2D array because in C++, when a 2D array of n rows and m columns is declared, then in the memory a 1D array of size n x m is created.
Method 4: Passing 2D Array as a Single Pointer.
return_type function(array_type *arr,int row,int col) //converting 2d array to pointer type by typecasting // in function call function((array_type *)array_name,row,col);
Example
The below program demonstrates passing a 2D Array as an argument using a single pointer.
C++
// C++ program to demonstrates passing a 2D Array as an // argument using a single pointer. #include <iostream> using namespace std; // function to print an array void printArr( int * arr, int n, int m) { for ( int i = 0; i < n; i++) { for ( int j = 0; j < m; j++) { // printing the value of an element cout << *((arr + i * m) + j) << " " ; } cout << endl; } } int main() { int arr[][2] = { { 20, 25 }, { 30, 35 }, { 40, 45 } }; int n = 3, m = 2; // calling print function printArr(( int *)arr, n, m); return 0; } |
20 25 30 35 40 45
How to Pass 2D Array to Functions in C++ ?
A two-dimensional array or a 2D array is an array having a collection of elements organized in rows and columns. It can be viewed as an array of arrays. 2D array can be visualized as a table or a grid, we can access elements of a 2D array using its row and column indices. In C++, we can pass a 2D array as an argument to a function.
Example of a 2D array
Below is an example of a 2D array having 3 rows and 2 columns.
int arr[3][2] = { {10 , 20}, {30 , 40}, {50 , 60}, };
Contact Us