How to Pass a Multidimensional Array to Function in C

Many times we need to write a C program with deals with multidimensional arrays, and for code reusability, we are using functions. Sometimes we need to pass those arrays to the functions. Here we will see how to pass a multidimensional array to function in C.

#include<stdio.h>
void display(int n[2][2]);
int main()
{
  int num[2][2], i, j;
  printf("Enter 4 Elements of array[2][2]: ");
  for(i=0;i<2;i++)
  {
    for(j=0;j<2;j++)
    {
      scanf("%d",&num[i][j]);
    }
  }
  printf("Displaying values: ");
  display(num);  
  // passing num[2][2] as num to display()

  return 0;
}

void display(int n[2][2])
{
  int i, j;
  for(i=0;i<2;i++)
  {
    for(j=0;j<2;j++)
    {
      printf("%d ",n[i][j]);
    }
  }
}

Output:-

Enter 4 values: 10
15
20
25
Displaying values: 10 15 20 25

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Examples on multidimensional Array in C

Leave a Comment

Your email address will not be published. Required fields are marked *