Wednesday, November 2, 2011

Pointer to an Array in C programming language

If we can have a pointer to an integer, a pointer to a float, a pointer to a char, then can we not have a pointer to an array? We certainly can. The following program shows how to build and use it.

/* Usage of pointer to an array */
main( )
{
      int  s[5][2] = {
     { 1234, 56 },
     { 1212, 33 },
     { 1434, 80 },
     { 1312, 78 }
                       } ;           
      int  ( *p )[2] ;
      int  i, j, *pint ;
 
      for ( i = 0 ; i <= 3 ; i++ )
 {
            p = &s[i] ;
            pint = p ;
            printf ( "\n" ) ;
            for ( j = 0 ; j <= 1 ; j++ )
                  printf ( "%d ", *( pint + j ) ) ; 
 }
}

And here is the output...

 1234  56
1212  33 
1434  80 
1312  78 

Here  p is a pointer to an array of two integers. Note that the parentheses in the declaration of p are necessary. Absence of them would make p an array of 2 integer pointers. Array of pointers is covered in a later section in this chapter. In the outer for loop each time we store the address of a new one-dimensional array. Thus first time through this loop p would contain the address of the zeroth 1-D array. This address is then assigned to an integer pointer pint. Lastly, in the inner for loop using the pointer pint wehave printed the individual elements of the 1-D array to which p is pointing. 
But why should we use a pointer to an array to print elements of a 2-D array. Is there any situation where we can appreciate its usage better? The entity pointer to an array is immensely useful when we need to pass a 2-D array to a function. This is discussed in the next section.

No comments:

Post a Comment