This declaration
int (*q)[4];
declares a pointer to objects of type int[4]
. To simplify the understanding you could introduce a typedef the following way
typedef int T[4];
T *q;
q = a;
So dereferencing the pointer you will get the pointed object of the type T
that represents an array of type int[4]
. As result using this object in the printf
function
printf("\n%d\t%d",*(++p),*(++q));
^^^^^
you will get the second "row" (due to incrementing the pointer ++q
) of the array a
that is in turn a one-dimensional array of type int[4]
that in turn is implicitly converted to pointer to its first element.
Thus the expression *(++q)
has the type int *
and points to the first element of the second "row" of the array a
.
If you want to use this pointer to traverse the elements of the array a
you can do it the following way
#include <stdio.h>
int main(void)
{
int a[][4] = { { 2, 3, 4, 5 }, { 43, 32, 76, 3 } };
int ( *q )[4] = a;
for ( size_t i = 0; i < sizeof( a ) / sizeof( *a ); i++ )
{
for ( size_t j = 0; j < sizeof( *q ) / sizeof( **q ); j++ )
{
printf( "%2d ", q[i][j] );
}
printf( "\n" );
}
return 0;
}
The program output is
2 3 4 5
43 32 76 3
That is q[0]
is the first "row" of the array a
. You can write also just *q
. q[1]
is the second "row" of the array a
. You may can write also like *( q + 1 )
. To get access to the elements of each row you can apply the subscript operator like (the first row)
q[0][i]
where i
is some index value. Or you can write the same like ( *q )[i]
and like (the second row) q[1][i]
or ( *( q + 1 ) )[i]
And the special expression like **q
yields the first element of the first row of the array a
.