It is tested and obvious that the following code won't work as we are initialising an array q[][4] with the address of first element of an array a.
int main()
{
int a[3][4] =
{
1, 2, 3, 4,
5, 6, 7, 8,
9, 0, 1, 6
} ;
int q[ ][4]=a;
int i,j;
for(i=0; i<3; i++)
{
for(j=0;j<4;j++)
{
printf("%d",q[i][j]);
}
}
return 0;
}
But, If we implement the same logic using functions(ie passing address of array a to a formal parameter q[][4] of a function) then it works fine.
main( )
{
int a[3][4] =
{
1, 2, 3, 4,
5, 6, 7, 8,
9, 0, 1, 6
} ;
print ( a, 3, 4 ) ;
}
print ( int q[ ][4], int row, int col )
{
int i, j ;
for ( i = 0 ; i < row ; i++ )
{
for ( j = 0 ; j < col ; j++ )
printf ( "%d ", q[i][j] ) ;
printf ( "\n" ) ;
}
printf ( "\n" ) ;
}
how come it is possible?