# How to pass a 2D array as a parameter In C?

In this below code structure you will understand while passing 2d array as parameter in C programming.

Before passing we must typecast the 2D array while passing the function.

## Here below implementation of 2D Array:

```#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[] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int m = 3, n = 3;

// We can also use "print(&arr, m, n);"
print((int *)arr, m, n);
return 0;
}```

Output:

``1 2 3 4 5 6 7 8 9  ``

### What is going on inside the code?

First declaration of an array:

``int arr[] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};``

Then we declare size of row and column like wise

``int m = 3, n = 3``

At line no. 16 we use print function as parameters passing array pointer with sizes of m and n.

``print((int *)arr, m, n);``

You can also write above parameter as &a, m, n as argument in place of (int *)arr, m, n.

The formal parameter declaring as:

``void print(int *arr, int m, int n)``

The we declare for loop for row and column like

``````for (i = 0; i < m; i++)
for (j = 0; j < n; j++)``````

Here i stands for indexing row and j stands for indexing column.

Then print 2D array elements by this line –

``printf("%d ", *((arr+i*n) + j));``

Here, *((arr+i*n) + j) print array elements according row and column wise which is similar to

*(*(arr+i)+j) . But return error i.e.

``````error: invalid type argument of unary ‘*’ (have ‘int’)
printf("%d ", *(*(arr+i) + j));``````

So, when write inside function as passing parameter 2D array we write the line in this ways.

Till now if you understand please comment and share this post and wants to improve WhatsApp me. 