In C++, arrays are always passed as pointers. The same goes for the two-dimensional arrays. There are multiple ways of passing a 2D array to function as discussed below.
Passing Rows and Columns
return_type name (array_type array_name[rows][columns], int rows, int col)
Example: Below program demonstrates passing a 2D Array with a 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;
}
Output
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.
Passing Rows and Columns Declared Globally.
return_type name (array_type array_name[rows][columns])
Example: The below program demonstrates passing a 2D Array with a known number of rows and columns that are 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;
}
Output
10 15 20 25 30 35
Passing only the Number of Columns
return_type name (array_type array_name[][columns])
or
return_type name (array_type (*array_name)[columns])
Example: Below program demonstrates passing a 2D Array with a known number of columns only.
#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;
}
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.
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: Below program 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;
}
Output
20 25 30 35 40 45