This declaration
int (*Aop[5])[4];
does not declare a pointer. It is a declaration of an array of 5 pointers to one-dimensional arrays of the the int[4]
.
Arrays do not have the assignment operator. However you could initialize it in its declaration as for example
int (*Aop[5])[4] = { &arr2[0], &arr2[1], &arr2[2], &arr2[3], &arr2[4] };
or
int (*Aop[5])[4] = { arr2, arr2 + 1, arr2 + 2, arr2 + 3, arr2 + 4 };
Here is a demonstrative program.
#include <stdio.h>
int main(void)
{
enum { M = 5, N = 4 };
int arr2[M][N] =
{
{ 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 },
{ 4, 4, 4, 4 },
{ 5, 5, 5, 5 }
};
int (*Aop[M])[N] = { arr2, arr2 + 1, arr2 + 2, arr2 + 3, arr2 + 4 };
for ( size_t i = 0; i < M; i++ )
{
for ( size_t j = 0; j < N; j++ )
{
printf( "%d ", ( *Aop[i] )[j] );
}
putchar( '\n' );
}
return 0;
}
The program output is
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
If you need to declare a pointer to the array arr2
then you should bear in mind that array designators are implicitly converted (with rare exceptions) to pointers to their first elements. The array arr2
has elements of the type int[4]
. So a pointer to an object of this type will have the type int ( * )[4]
. So you can write
int (*Aop )[4] = arr2;
Here is another demonstrative program that uses pointers in for loops to output elements of the array arr2
.
#include <stdio.h>
int main(void)
{
enum { M = 5, N = 4 };
int arr2[M][N] =
{
{ 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 },
{ 4, 4, 4, 4 },
{ 5, 5, 5, 5 }
};
int (*Aop )[N] = arr2;
for ( int ( *p )[N] = Aop; p != Aop + M; ++p )
{
for ( int *q = *p; q != *p + N; ++q )
{
printf( "%d ", *q );
}
putchar( '\n' );
}
return 0;
}
Again the program output is
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5