The following code works partially. Kindly help to figure out the mistake
#include<stdio.h>
#include<stdlib.h>
void show ( int ( *q )[4], int row, int col )
{
int i, j ;
for ( i = 0 ; i < row ; i++ )
{
q=q+i;
for ( j = 0 ; j < col ; j++ )
printf ( "%d ", * ( *q + j ) ) ;
printf ( "\n" ) ;
}
printf ( "\n" ) ;
}
int main( )
{
int a[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 0, 1, 6}
} ;
show ( a, 3, 4 ) ;
return 0;
}
I am able to print only the first two subarrays.For the 3rd(last) subarray i get junk values
Additional Ques
In the above code-
show ( a, 3, 4 ) ; Through this statement I am passing 'a'. In this case 'a' contains the address of first subarray a[0]. This a[0] address in stored in (*q)[4].
In show() since q contains a[0] address i am looping through and printing all elements in first sub array (i.e) 1,2,3,4
In show() again using statement q++, the value of q is changed to point to a[1] (i.e) second sub-array. Then through looping all the elements in 2nd subarray are printed. This process continues.
Here 'a' the name of the 2D array stores 'a[0]' first-sub array address.
a[0] the first sub array stores the first element a[0][0] address and so on.
Question:
When my 2D array is created space is allocated for these 'a','a[0]','a[1]','a[2]','a[3]' Isn't it?? Apart from space allocated to a[0][0],a[0][1],a[1][0]......a[3][4]
Why I am not able to retrieve the address of 'a' and 'a[0]','a[1]','a[2]','a[3]'. Whenever & is associated with them i get address of a[0][0],a[1][0],a[2][0]