I have doubt in syntax of pointer to array and 2D array
#include<stdio.h>
#include<stdlib.h>
void show ( int q[][4], int row )
{
int i, j ;
for ( i = 0 ; i < row ; i++ )
{
for ( j = 0 ; j < 4 ; j++ )
printf ( "%d ", q[i][j] ) ;
printf ( "\n" ) ;
}
printf ( "\n" ) ;
}
int main( )
{
int a[][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 0, 1, 6}
} ;
show ( a, 3, 4 ) ;
return 0;
}
The above code works with all the below notations
void show ( int q[][4], int row )
void show ( int q[3][4], int row )
void show ( int ( *q )[4], int row, int col )
int q[][4] == int q[3][4]==int ( *q )[4] // represents pointer to 1D array with size 4
I have read this
int q[ ][4];
This is same as int ( *q )[4], where q is a pointer to an array of 4
integers. The only advantage is that we can now use the more
familiar expression q[i][j] to access array elements.
Question:
The show() function works with both of these, int q[][4] and int q[3][4] to receive 2D array's address.
But these are representation for 2D array right?
we can't allocate 2D like the following
int (*a)[4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 0, 1, 6}
} ;
But 2D can be allocated via this statement
int a[][4]={
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 0, 1, 6}
} ;
int (*a)[4] is not the same as int a[][4], then how come both 'pointer to array' and '2D array notation' can be used in show function?
int(*q)[4]and intq[][4],q[i][j]is fine.