# Can Multidimensional Arrays be Passed as Function Arguments in C?

«
»
This C Tutorial Explains Multidimensional Arrays Passed as Function Arguments in C with Example(s).

Let’s, first, take an example of a 2-dimensional array,

```    int hawks = {{1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15},
{16,17,18,19,20},
};     /* hawks's initialized here */```

In order to pass ‘hawks’ as a function argument, we must know type of array ‘hawks’, so that we would be able to declare correctly formal parameter for this in function prototype and in function header. What type of array ‘hawks’ is? ‘hawks’ is an array of 4 arrays of 5 integers each. Then, how to determine type of ‘hawks’? Because, ‘hawks’ is an array of 4 arrays of 5 integers, therefore each element of ‘hawks’ is an array of 5 integers. So, pointer declared to ‘hawks’ should be one which points to an array of 5 integers, i.e. an element of ‘hawks’. Let’s write pointer declaration as,

`    int (*ptr);      /* 'ptr', a pointer-to-array-of-5-integers */`

O key! Now, we try accessing array ‘hawks’ in function “access_multidimarr(int (*ptr), int num)”. Note, here, that we have to pass number of arrays of integers i.e. no. of elements ‘hawks’ has to function since function “access_multidimarr()” can’t in any way determine what many elements to process. Let’s take a program with this function to access array ‘hawks’ in the function and observe the output,

```/*
* multidimarr_fun_arg.c -- program shows if we can pass multidimensional
* array as function argument
*/
#include <stdio.h>
void access_multidimarr(int (*), int);    /* prototype */

int main(void)
{
int hawks = {{1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15},
{16,17,18,19,20},
};        /* hawks's initialized */

printf("Now we access \'hawks\'...\n");
access_multidimarr(hawks, 4);      /* called the function */
return 0;
}

void access_multidimarr(int (*ptr), int num)
{
int i, j;

printf("In function: accessing \'hawks\' using pointer to array.\n");
for (i = 0; i < num; i++) {
for (j = 0; j < 5; j++) {
printf("hawks[%d][%d] is %d\n", i, j, *(*ptr + j));
}
ptr++;
}
}```

Here’s the output:

```Now we access 'hawks'...
In function: accessing 'hawks' using pointer to array.
hawks is  1
hawks is  2
hawks is  3
hawks is  4
hawks is  5
hawks is  6
hawks is  7
hawks is  8
hawks is  9
hawks is 10
hawks is 11
hawks is 12
hawks is 13
hawks is 14
hawks is 15
hawks is 16
hawks is 17
hawks is 18
hawks is 19
hawks is 20```

So, we, here, learned how we passed a multi-dimensional array, ‘hawks’, as a function argument and accessed the same in the function. 