Pass 2D array as a function parameter in C++
This post will discuss how to pass a 2D array as a function parameter in the C++ programming language.
1. Static Array
If we know the array dimensions at compile-time, we can pass the array by reference using a function template in C++, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <iostream> #define M 5 #define N 5 // create a function template template <size_t r, size_t c> void assign(int (&arr)[r][c]) { for (size_t i = 0; i < r; i++) { for (size_t j = 0; j < c; j++) { arr[i][j] = i + j; } } } // Program to pass the 2D array as a function parameter in C++ int main() { int arr[M][N]; assign(arr); // print 2D array return 0; } |
The advantage of using this approach is that there is no need to specify the array dimensions. The disadvantage of using this approach is that we can’t use it with dynamic arrays.
We can also create a wrapper over the function, as shown below, which is more efficient for large arrays:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <iostream> #define M 5 #define N 5 void assign(int *arr, int r, int c) { for (size_t i = 0; i < r; i++) { for (size_t j = 0; j < c; j++) { arr[i * c + j] = i + j; } } } // create a function template template <size_t r, size_t c> void assign(int (&arr)[r][c]) { assign(*arr, r, c); } // Program to pass the 2D array as a function parameter in C++ int main() { int arr[M][N]; assign(arr); // print 2D array return 0; } |
2. 1D Array
We can even use a 1D array to allocate memory for a 2D array by allocating one huge block of M×N memory. This can be done both statically and dynamically, as shown below:
⮚ Static 1D Array
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> #define M 4 #define N 5 void assign(int A[]) { for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { *(A + i*N + j) = i + j; } } } // Program to pass the 2D array as a function parameter in C++ int main() { // allocate memory of size `M × N` int A[M * N]; assign(A); // print the 2D array return 0; } |
⮚ Dynamic 1D Array
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <iostream> #define M 4 #define N 5 void assign(int *A) { for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { *(A + i*N + j) = i + j; } } } // Program to pass the 2D array as a function parameter in C++ int main() { // dynamically allocate memory of size `M × N` int* A = new int[M * N]; assign(A); // print the 2D array // deallocate memory delete[] A; return 0; } |
3. Array of Pointers
When the parameter is an array of pointers, we can do something like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#include <iostream> #define M 4 #define N 5 void assign(int **A) { for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { A[i][j] = i + j; } } } // Program to pass the 2D array as a function parameter in C++ int main() { // dynamically create an array of pointers of size `M` int** A = new int*[M]; // dynamically allocate memory of size `N` for each row for (int i = 0; i < M; i++) { A[i] = new int[N]; } // assign values to the allocated memory assign(A); // print the 2D array // deallocate memory using the `delete[]` operator for (int i = 0; i < M; i++) { delete[] A[i]; } delete[] A; return 0; } |
That’s all about passing a 2D array as a function parameter in C++.
Also See:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)