A 2-D array is an array of arrays, where each element of the main array represents another array (a row). When passing a 2-D array to a function, its dimensions must be specified.
- The most common way is to declare the function parameter as a 2-D array, specifying the column size.
- Since array size information is not retained when passed to a function, the required dimensions must be provided separately.
#include <stdio.h>
// Funtion that takes 2d array as parameter
void print(int arr[3][3], int n, int m)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
printf("%d ", arr[i][j]);
printf("\n");
}
}
int main()
{
int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
// Passing the array along with the size of rows and columns
print(arr, 3, 3);
return 0;
}
Output
1 2 3 4 5 6 7 8 9
Explanation
- The 2D array arr is passed to the print() function in the same way it is declared, but without specifying the row size.
- The column size must be specified in the function parameter so the compiler can correctly access the array elements.
- The array dimensions are passed separately to the function since array size information is not retained when passing an array.
In the above method, the size of the row dimension can be omitted, and the program will still work.
#include <stdio.h>
// Funtion that takes 2d array as parameter
void print(int arr[][3], int n, int m)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
printf("%d ", arr[i][j]);
printf("\n");
}
}
int main()
{
int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
// Passing the array along with the size of rows and columns
print(arr, 3, 3);
return 0;
}
Output
1 2 3 4 5 6 7 8 9
As Pointer to an Array
When a 2-D Arrays is passed to a function, it decays into a pointer to its first element. Therefore, the function parameter can be declared as a pointer to an array with the specified column size.
- The function parameter is declared as a pointer to an array, where the column size must be specified.
- This method works because a 2-D array decays into a pointer to its first row when passed to a function.
#include <stdio.h>
// Funtion that takes 2d array as parameter
void print(int (*arr)[3], int n, int m)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
printf("%d ", arr[i][j]);
printf("\n");
}
}
int main()
{
int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
// Passing the array along with the size of rows and columns
print(arr, 3, 3);
return 0;
}
Output
1 2 3 4 5 6 7 8 9
As Single Level Pointer
Since a 2-D array is stored in contiguous memory locations, it can also be passed to a function as a single-level pointer. This method relies on the array's internal memory layout.
- The function treats the 2-D array as a 1-D array, accessing elements based on their sequential storage.
- This approach works only when the internal storage order of the array is known.
#include <stdio.h>
void print(int *arr, int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
printf("%d ", *(arr + i * cols + j));
printf("\n");
}
}
int main()
{
int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
// Passing the array as a pointer along with the size of rows and columns
print((int *)arr, 3, 3);
return 0;
}
Output
1 2 3 4 5 6 7 8 9
This method is not generally preferred as it is highly error prone.