Introduction
C is a procedure-oriented programming language created by Dennis Ritchie in 1972. It provides us with a feature called - “functions” to separate code from the main function. Functions in C can help us reuse the code instead of writing the lengthy and similar codes frequently for the same functionality. We can call the function any number of times and from anywhere to access the code inside it. We pass variables as arguments to the functions to access them inside the functions.
The variables inside the invoked function are called actual parameters, and the arguments in the function header are called formal parameters. Let’s learn how to pass a 2D array as a parameter to functions in C.
Also See: procedure call in compiler design, C Static Function
Passing 2D array as parameter
We can pass an entire 2D Array as a parameter to a function and access it inside the function. There are two ways to pass a 2D array as a parameter
- As an array
- As a pointer
As an array
A 2D array can be passed using many methods. One of the common methods is passing it as an array with dimensions, but using this method can increase the time complexity of the code. The time complexity to pass a 2D array as an argument is O(N^2). We can pass the array as a parameter, as shown below.
#include<stdio.h>
void assignValues(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;
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
printf("%3d", arr[i][j]);
}
printf("\n");
}
}
// main function
int main(void) {
int m = 5, n = 5;
int arr[m][n];
assignValues(m, n, 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
We initialized the dimensions(row and column size) of the array statically in this code. We invoked a function assignValues by passing the row, columns size, and the entire 2D array as parameters. Then we assign the value to the array variables and print the array.
Click on the following link to read further: Features of C Language and Short int in C Programming
As a pointer
A 2D array can be passed using many methods. One of the common methods is passing it as a pointer. Pointers store the address of the variable, so changing the value of a variable from anywhere will also change the actual value. We can create an array of pointers and dynamically allocate the memory as shown below:
#include<stdio.h>
void assign(int** arr, int a, int b) {
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
arr[i][j] = i + j;
}
}
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
printf("%3d", arr[i][j]);
}
printf("\n");
}
}
//main function
int main(void) {
int a = 5;
int b = 5;
int **arr = (int **)malloc(a * sizeof(int *));
for (int r = 0; r < a; r++) {
arr[r] = (int *)malloc(b * sizeof(int));
}
assign(arr, a, b);
// deallocate memory
for (int i = 0; i < a; 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
We initialized the dimensions(row and column size) of the array in the code and declared the array dynamically as a pointer, so the size of the array could be flexible and modified. We invoked a function assignValues by passing the row, columns size, and the array pointer as arguments. Then we assign the value to the array variables and print the array. We can also print the array from the main function without returning any values to the main, as the value gets changed according to the address.
You can also read about dynamic array in c, And Tribonacci Series
Must Read what is storage class in c