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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- How to print size of array parameter in C++?
- Parameter Passing Techniques in C/C++
- Difference between Argument and Parameter in C/C++ with Examples
- How to pass an array by value in C ?
- When do we pass arguments by reference or pointer?
- What’s difference between “array” and “&array” for “int array[5]” ?
- Pointer to an Array | Array Pointer
- array::front() and array::back() in C++ STL
- array::fill() and array::swap() in C++ STL
- array::begin() and array::end() in C++ STL
- array::crbegin() and array::crend() in C++ STL
- array::cbegin() and array::cend() in C++ STL
- array::rbegin() and array::rend() in C++ STL
- Difference between pointer to an array and array of pointers
- Jagged Array or Array of Arrays in C with Examples
- Array of Structures vs. Array within a Structure in C/C++
- Why C treats array parameters as pointers?
- Pointer vs Array in C
- Do not use sizeof for array parameters
- What is the difference between single quoted and double quoted declaration of char array?
Improved By : BabisSarantoglou

