Wednesday, November 2, 2011

Passing 2-D Array to a Function in C programming language

There are three ways in which we can pass a 2-D array to a function. These are illustrated in the following program.

/* Three ways of accessing a 2-D array */

main( )
{
      int  a[3][4] =   {
     1, 2, 3, 4,
     5, 6, 7, 8,
     9, 0, 1, 6
    } ;

      clrscr( ) ;
      display ( a, 3, 4 ) ;
      show ( a, 3, 4 ) ;
      print ( a, 3, 4 ) ;
}
display ( int  *q, int  row, int  col )
{
      int  i, j ;

      for ( i = 0 ; i < row ; i++ )
 {
            for ( j = 0 ; j < col ; j++ )
                  printf ( "%d ", * ( q + i * col + j ) ) ;
printf ( "\n" ) ;
 }
      printf ("\n" ) ;
}  
show ( int  ( *q )[4], int  row, int  col )
{
      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" ) ;
 }
      printf ( "\n" ) ;
}

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" ) ;
}

And here is the output…

1 2 3 4
5 6 7 8
9 0 1 6

1 2 3 4
5 6 7 8
9 0 1 6

1 2 3 4
5 6 7 8
9 0 1 6

In the display( ) function we have collected the base address of the 2-D array being passed to it in an ordinary int pointer. Then through the two for loops using the expression * ( q + i * col + j ) we have reached the appropriate element in the array. Suppose i is equal to 2 and j is equal to 3, then we wish to reach the element a[2][3]. Let us see whether the expression * ( q + i * col + j ) does give this element or not. Refer Figure 8.7 to understand this.



The expression * ( q + i * col + j ) becomes * ( 65502 + 2 * 4 + 3). This turns out to be * (65502 + 11 ). Since 65502 is address of an integer, * ( 65502 + 11 ) turns out to be * (65524). Value at this address is 6. This is indeed same as a[2][3]. A more general formula for accessing each array element would be:

* ( base address + row no. * no. of columns + column no. )

In the show( ) function we have defined q to be a pointer to an array of 4 integers through the declaration:

int  ( *q )[4] ;

To begin with, q holds the base address of the zeroth 1-D array, i.e. 4001 (refer Figure 8.7). This address is then assigned to p, an int pointer, and then using this pointer all elements of the zeroth 1-D array are accessed. Next time through the loop when i takes a value 1, the expression q + i fetches the address of the first 1-D array. This is because, q is a pointer to zeroth 1-D array and adding 1 to it would give us the address of the next 1-D array. This address is once again assigned to p, and using it all elements of the next 1-D array are accessed. In the third function print( ), the declaration of q looks like this: 

int  q[ ][4] ;

This is same as int  ( *q )[4], where q is 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. We could have used the same expression in show( ) as well.

No comments:

Post a Comment

Post a Comment