Reputation: 115
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]
Upvotes: 0
Views: 400
Reputation: 1206
Try this one. In the below code that there are simple take a pointer and assign the address in it, and print according to that.Because this address hold the address of base address of array q+i
and after that it increment from there offset.
int i, j;
int *p;
for (i = 0; i < row; i++) {
p = q + i;
for (j = 0; j < col; j++) {
printf("%d ", *(p + j));
}
printf("\n");
}
Upvotes: 0
Reputation: 839054
The error is here:
q=q+i;
This first increases q by zero, then by one, then by two causing it to miss out the third element and jump straight to the fourth (and in your case this indexes past the end of the array, giving the junk values).
You need to increment by one each time q++;
and you will need to put this at the end of the loop. Alternatively you can remove this line completely and use this instead:
for (j = 0; j < col; j++) {
printf("%d ", q[i][j]);
}
Upvotes: 4
Reputation: 61643
q=q+i;
Let's trace what happens to q
each time through the loop.
When i == 0
, q
has the original value.
When i == 1
, we add 1
to q
.
When i == 2
(the last row), we add 2
to q
. However, we already added 1
in the previous iteration. So now q
is 3 more than it was to begin with, and thus points just past the end of the array. When we dereference this pointer, we get undefined behaviour.
Upvotes: 0
Reputation: 409442
Why not use something simple:
void show ( int q[][4], int row )
{
int i, j ;
for ( i = 0 ; i < row ; i++ )
{
/* We already know there are four "columns", no need for a parameter */
for ( j = 0 ; j < 4 ; j++ )
printf ( "%d ", q[i][j] ) ;
printf ( "\n" ) ;
}
printf ( "\n" ) ;
}
Upvotes: 2