Passing 2D Array using Array Notation in C

Notes:

Passing Array (2D) to Function in C using Array notation:
Passing 2D ( two dimensional ) array to a function: using array notation:

2D Array:
- 2D array is a collection of homogeneous data elements arranged in rows and columns
- 2D array is an array of 1D arrays

Declaring and initializing a 2D array:
- There are 2 different ways in which we can declare and initialize 2D arrays

1. data_type arrayName[rows][cols] = {list of values separated by comma};
Ex: int a[2][2] = {1,2,3,4}; OR int a[][2] ={1,2,3,4};

2. data_type arrayName[rows][cols] = {blocks of values separated by comma};
Ex: int a[2][2] = {{1,2},{3,4}}; OR int a[][2] = {{1,2},{3,4}};

Note: writing # of rows to be created is optional

Note:
1. Name of a 1D array represents address of the first element in the 1D array
2. Name of a 2D array represents address of the first row in the 2D array

W.K.T. There are 2 different notations for passing an array to a function:

1. Array notation: [ ]
- basic syntax of declaring a function; for accepting 2D array using array notation
return_type functionName(datatype paramName[ ][cols]);
Ex:
void display(int array[ ][2],int rows, int cols);

2. Pointer notation: *
- basic syntax of declaring a function; for accepting 2D array using pointer notation
return_type functionName(datatype (*paramName)[cols]);
Ex:
void display(int (*array)[2],int rows, int cols);

Note: While passing a 2D array to a function; it is also recommended to pass # of rows & # of columns in it

Example Code: Passing 2D array to a function using array notation

#include <stdio.h>

void display(int array[][2],int rows,int cols);

int main()
{
int a[][2] = {
{1,2},
{3,4}
};

display(a,2,2);

return 0;
}

void display(int array[][2],int rows,int cols)
{
int i=0,j=0;

for(i=0; i<rows; i++)
{
for(j=0; j<cols; j++)
{
printf("%d ", array[i][j]);
}
printf("\n");
}
}