How to Pass 2D Array to Functions in C++ ?
Last Updated :
02 Jan, 2024
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},
};
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++
#include <iostream>
using namespace std;
void printArr( int arr[3][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()
{
const int n = 3, m = 2;
int arr[n][m] = { { 10, 15 }, { 20, 25 }, { 30, 35 } };
printArr(arr, n, m);
return 0;
}
|
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++
#include <iostream>
using namespace std;
const int n = 3, m = 2;
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 } };
printArr(arr);
return 0;
}
|
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++
#include <iostream>
using namespace std;
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 } };
int n = sizeof arr / sizeof arr[0];
printArr(arr, n, m);
return 0;
}
|
Output
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++
#include <iostream>
using namespace std;
void printArr( int * arr, int n, int m)
{
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++) {
cout << *((arr + i * m) + j) << " " ;
}
cout << endl;
}
}
int main()
{
int arr[][2] = { { 20, 25 }, { 30, 35 }, { 40, 45 } };
int n = 3, m = 2;
printArr(( int *)arr, n, m);
return 0;
}
|
Share your thoughts in the comments
Please Login to comment...