How to pass a 2D array as a parameter in C?
This post is an extension of How to dynamically allocate a 2D array in C?
A one dimensional array can be easily passed as a pointer, but syntax for passing a 2D array to a function can be difficult to remember. One important thing for passing multidimensional arrays is, first array dimension does not have to be specified. The second (and any subsequent) dimensions must be given
1) When both dimensions are available globally (either as a macro or as a global constant).
#include <stdio.h> const int M = 3; const int N = 3; void print(int arr[M][N]) { int i, j; for (i = 0; i < M; i++) for (j = 0; j < N; j++) printf("%d ", arr[i][j]); } int main() { int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; print(arr); return 0; } |
Output:
1 2 3 4 5 6 7 8 9
2) When only second dimension is available globally (either as a macro or as a global constant).
#include <stdio.h> const int N = 3; void print(int arr[][N], int m) { int i, j; for (i = 0; i < m; i++) for (j = 0; j < N; j++) printf("%d ", arr[i][j]); } int main() { int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; print(arr, 3); return 0; } |
Output:
1 2 3 4 5 6 7 8 9
The above method is fine if second dimension is fixed and is not user specified. The following methods handle cases when second dimension can also change.
3) If compiler is C99 compatible
From C99, C language supports variable sized arrays to be passed simply by specifying the variable dimensions (See this for an example run)
// The following program works only if your compiler is C99 compatible. #include <stdio.h> // n must be passed before the 2D array void print(int m, int n, int arr[][n]) { int i, j; for (i = 0; i < m; i++) for (j = 0; j < n; j++) printf("%d ", arr[i][j]); } int main() { int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int m = 3, n = 3; print(m, n, arr); return 0; } |
Output on a C99 compatible compiler:
1 2 3 4 5 6 7 8 9
If compiler is not C99 compatible, then we can use one of the following methods to pass a variable sized 2D array.
4) Using a single pointer
In this method, we must typecast the 2D array when passing to function.
#include <stdio.h> void print(int *arr, int m, int n) { int i, j; for (i = 0; i < m; i++) for (j = 0; j < n; j++) printf("%d ", *((arr+i*n) + j)); } int main() { int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int m = 3, n = 3; // We can also use "print(&arr;[0][0], m, n);" print((int *)arr, m, n); return 0; } |
Output:
1 2 3 4 5 6 7 8 9
References:
http://www.eskimo.com/~scs/cclass/int/sx9a.html
This article is contributed by Abhay Rathi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Recommended Posts:
- How to print size of array parameter in C++?
- How to pass an array by value in C ?
- Parameter Passing Techniques in C/C++
- When do we pass arguments by reference or pointer?
- Difference between pointer to an array and array of pointers
- array::cbegin() and array::cend() in C++ STL
- array::rbegin() and array::rend() in C++ STL
- array::fill() and array::swap() in C++ STL
- array::front() and array::back() in C++ STL
- array::crbegin() and array::crend() in C++ STL
- What’s difference between “array” and “&array” for “int array[5]” ?
- array::begin() and array::end() in C++ STL
- Sum of an array using MPI
- STD::array in C++
- array::at() in C++ STL
Improved By : BabisSarantoglou



