0

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:

  1. 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]

  2. 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]

intex0075
  • 115
  • 1
  • 7
  • For your edited question. No, arrays are not pointers and space is allocated contiguously for 12 elements. A 2D array is nothing than an "array of array". The address of an array is always the same as the address of its first element. For all these reasons the solution Joachim has given is a much better and simpler approach than what you are doing here. – Jens Gustedt Dec 30 '11 at 12:10

4 Answers4

4

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]);
}
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
2

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" ) ;
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 2
    +1, for using a 2D array simply as a 2D array. But I would go further (using C99 features) and just declare the function `void show(size_t row, size_t col, int q[row][col])`. – Jens Gustedt Dec 30 '11 at 10:12
  • http://stackoverflow.com/questions/16943909/manipulate-multidimensional-array-in-a-function – Dchris Jun 05 '13 at 16:05
0

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");
}
Varun Chhangani
  • 1,116
  • 3
  • 13
  • 18
0
q=q+i;

Let's trace what happens to q each time through the loop.

  1. When i == 0, q has the original value.

  2. When i == 1, we add 1 to q.

  3. 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.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • Thanks you. One more doubt. I am passing a[0] address using which incrementing to a[1] then a[2] to print values in each sub-array. Does it mean memory is created for a,a[0],a[1],a[2]also ?? apart from a[0][0]...a[3][3].But i am not able to find the address of those when i use & with them it points to the address of first element in each subarray – intex0075 Dec 30 '11 at 10:31
  • 1
    Sorry, I can't understand your question at all. – Karl Knechtel Dec 30 '11 at 10:51
  • :I have edited the question. Kindly clarify. Let me know if still my question is unclear. – intex0075 Dec 30 '11 at 11:50