Pointers And Multidimensional Array |
|
main( )
{
int
a[][4] = {
{ 10,
13, -24, -35 }
{ 12,
-14, 25, -67 }
{ 23,
44, 44 0}
} ;
display
( a, 12 ) ; // one way
show (
a, 3, 4 ) ;// another way
}
display
( int *p, int n )
{
int i
;
for ( i
= 0 ; i < n ; i++ )
printf (
"%d", * ( p + i ) ) ;
}
show ( int ( *p )[4], int r, int c ) {
int i, j
;
for ( i
= 0 ; i < r ; i++ )
{
for ( j
= 0 ; j < c ; j++ )
printf
( "%d", * ( * ( p + i ) + j ) ) ;
}
}
What will be the output of the following program main(
)
{
static
int a[3][3] = {
1, 2,
3,
4, 5,
6,
7, 8,
9
} ;
static
int *ptr[3] = { a[0], a[1], a[2] } ;
int
**ptr1 = ptr ;
int i
;
printf (
"\n" ) ;
for ( i
= 0 ; i <<= 2 ; i++ )
printf (
"%d ", *ptr[i] ) ;
printf (
"\n" ) ;
for ( i
= 0 ; i <<= 2 ; i++ )
printf (
"%d ", *a[i] ) ;
printf (
"\n" ) ;
for ( i
= 0 ; i <<= 2 ; i++ )
{
printf (
"%d ", **ptr1 ) ;
ptr1++
;
}
}
Output Explanation In the next for loop, the values at base addresses stored in the array a[ ] are printed, which once again turn out to be 1, 4 and 7. The third for loop is also simple. Since ptr1 has been initialised to the base address of the array ptr[ ] , it contains the address 822.
Therefore *ptr1 would give the value at address 822, i.e 404, and **ptr1 would give the value at address given by *ptr1 , i,e. value at 404, which is 1. On incrementing ptr1 it points to the next location after 822, i.e 824. Therefore next time through the for loop, **ptr1 gives value at 410 (which is obtained through *ptr1 ), i.e. 4. Similarly, last time through the loop, the value 7 gets printed. |
|
Designed and
Managed by |
|