Pass 2D array to a function as a parameter in C
This post will discuss how to pass a 2D array to a function as a parameter in C.
In the previous post, we have discussed how to allocate memory for a 2D array dynamically. This post will discuss how we can pass a 2D array to a C programming language function.
1. For Static Array
If we know the array bounds at compile-time, we can pass a static 2D array to a function 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 29 30 31 32 33 |
#include <stdio.h> #define M 5 #define N 5 // Here the parameter is a static 2D array void assign(int arr[M][N]) // or, use `arr[][N]` { for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { arr[i][j] = i + j; } } } // Program to pass the 2D array to a function in C int main(void) { int arr[M][N]; assign(arr); // print 2D array for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { printf("%3d", arr[i][j]); } printf("\n"); } return 0; } |
Output:
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
If we don’t know the array bounds at compile-time, we need to pass the array bounds to the function, as shown below. This will only work if the array is passed after the indices to the function.
|
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 |
#include <stdio.h> // Here the parameter is a static 2D array void assign(int m, int n, int arr[m][n]) { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { arr[i][j] = i + j; } } } // Program to pass the 2D array to a function in C int main(void) { int m = 5; int n = 5; int arr[m][n]; assign(m, n, arr); // print 2D array for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { printf("%3d", arr[i][j]); } printf("\n"); } return 0; } |
Output:
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
2. Using Array of Pointers
When the array bounds are not known until runtime, we can dynamically create an array of pointers and dynamically allocate memory for each row. Then we can pass the array of pointers to a function, 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
#include <stdio.h> #include <stdlib.h> // Here, the parameter is an array of pointers void assign(int** arr, int m, int n) { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { arr[i][j] = i + j; } } } // Program to pass the 2D array to a function in C int main(void) { int m = 5; int n = 5; // dynamically create an array of pointers of size `m` int **arr = (int **)malloc(m * sizeof(int *)); // dynamically allocate memory of size `n` for each row for (int r = 0; r < m; r++) { arr[r] = (int *)malloc(n * sizeof(int)); } assign(arr, m, n); // print 2D array for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { printf("%3d", arr[i][j]); } printf("\n"); } // deallocate memory for (int i = 0; i < m; i++) { free(arr[i]); } free(arr); return 0; } |
Output:
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
3. Using 1D array
We can even use a 1D array to allocate memory for a 2D array. This can be done both statically and dynamically, as shown below:
⮚ Static 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 31 32 33 34 35 |
#include <stdio.h> #include <stdlib.h> // The parameter is a static 1D array void assign(int m, int n, int arr[m]) { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { arr[i*n + j] = i + j; } } } // Program to pass the 2D array to a function in C int main(void) { int m = 5; int n = 5; int arr[m * n]; assign(m, n, arr); // print 2D array for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { printf("%3d", arr[i*n + j]); } printf("\n"); } return 0; } |
Output:
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
⮚ Dynamic 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 31 32 33 34 35 36 37 38 |
#include <stdio.h> #include <stdlib.h> // Here, the parameter is a dynamic 1D array void assign(int* arr, int m, int n) { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { arr[i * n + j] = i + j; } } } // Program to pass the 2D array to a function in C int main(void) { int m = 5; int n = 5; int *arr = (int *)malloc(m * n * sizeof(*arr)); assign(arr, m, n); // print 2D array for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { printf("%3d", arr[i * n + j]); } printf("\n"); } // deallocate memory free(arr); return 0; } |
Output:
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
4. Using Double Pointer
If the function parameter is a pointer to a pointer, 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 41 42 43 44 45 46 47 48 49 |
#include <stdio.h> #include <stdlib.h> // Here, the parameter is a pointer to a pointer void assign(int **arr, int m, int n) { // assign values to the allocated memory for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { *(*(arr + i) + j) = i + j; } } } // Program to pass the 2D array to a function in C int main(void) { int m = 5; int n = 5; // dynamically create an array of pointers of size `m` int **arr = (int **)malloc(m * sizeof(int *)); // dynamically allocate memory of size `m×n` and let *arr point to it *arr = (int *)malloc(sizeof(int) * m * n); // position allocated memory across `m` pointers for (int i = 0; i < m; i++) { arr[i] = (*arr + n*i); } assign(arr, m, n); // print the 2D array for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { printf("%d ", *(*(arr + i) + j)); } printf("\n"); } // deallocate memory free(*arr); free(arr); return 0; } |
Output:
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
That’s all about passing a 2D array to a function as a 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 :)