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++
// 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;
}
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;
}
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;
}
Output100 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;
}
Similar Reads
Pass Array to Functions in C++
In C++, a collection of elements stored in contiguous memory locations and having the same data type is called an array. Passing arrays to functions is done to perform various operations on array elements without messing up with the main code. In C++, an array can be passed in a function using a poi
5 min read
Pass Array to Functions in C
Passing an array to a function allows the function to directly access and modify the original array. In this article, we will learn how to pass arrays to functions in C.In C, arrays are always passed to function as pointers. They cannot be passed by value because of the array decay due to which, whe
3 min read
Passing Vector to a Function in C++
To perform operations on vector belonging to one function inside other function, we pass this vector to the function as arguments during the function call.C++ provides three methods to pass a vector to a function in C++. Let's look at each of them one by one.Table of ContentPass Vector by ValuePass
5 min read
array get() function in C++ STL
The array::get() is a built-in function in C++ STL which returns a reference to the i-th element of the array container. Syntax: get(array_name) Parameters: The function accepts two mandatory parameters which are described below. i - position of an element in the array, with 0 as the position of the
2 min read
Passing Pointers to Functions In C++
Prerequisites: Pointers in C++Functions in C++ Passing Pointers to functions means declaring the function parameter as a pointer, at the function calling passing the address of the variable and that address will be stored by a parameter that is declared as a pointer. To change the value of any varia
4 min read
How to pass or return a structure to/from a Function in C/C++?
A structure is a user-defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type. How to pass structure as an argument to the functions? Passing of structure to the function can be done in two ways: By passing all the el
3 min read
Passing a Vector to Constructor in C++
Just like any other data, we can also pass a vector to a constructor of the desired class. We can pass it by value or by reference. What we do with it depends on our requirement. The following are some of the common cases:Copy Data to Member VectorIf we want to store the copy of the vector in the cl
3 min read
How to copy elements of an Array in a Vector in C++
An Array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. Vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted, with their storage being handled
6 min read
C++ Return 2D Array From Function
An array is the collection of similar data-type stored in continuous memory. And when we are storing an array inside an array it is called 2 D array or 2-dimensional array. To know more about arrays refer to the article Array in C++. When there is a need to return a 2D array from a function it is al
4 min read
Pointer to an Array in C++
Pointers in C++ are variables that store the address of another variable while arrays are the data structure that stores the data in contiguous memory locations. In C++, we can manipulate arrays by using pointers to them. These kinds of pointers that point to the arrays are called array pointers or
6 min read