This
int *array[10]
is a declaration of an array with 10 elements of the type int *
.
Here is a demonstrative program.
#include <stdio.h>
int main(void)
{
enum { N = 10 };
int a[N] =
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
};
int * array[N] =
{
&a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7], &a[8], &a[9]
};
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", *array[i] );
}
putchar( '\n' );
return 0;
}
Its output is
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
This
int (*array)[10]
is a declaration of a pointer to an object having the array type int[10]
.
Here is another demonstrative program.
#include <stdio.h>
int main(void)
{
enum { N = 10 };
int a[N] =
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
};
int (*array)[N] = &a;
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", ( *array )[i] );
}
putchar( '\n' );
return 0;
}
Its output is the same as above.
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
This
int *(*array)[10]
is a declaration of a pointer to an object of the array type int *[10]
.
Here is one more demonstrative program.
#include <stdio.h>
int main(void)
{
enum { N = 10 };
int a[N] =
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
};
int * b[N] =
{
&a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7], &a[8], &a[9]
};
int *(*array)[N] = &b;
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", *( *array )[i] );
}
putchar( '\n' );
return 0;
}
Again its output the same as shown above.
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
In general if you have an array for example like this
T a[N1][N2][N3];
where T
is some type specifier and N1
, N2
, N3
are its sizes then a pointer to such an array will look like
T ( *p )[N1][N2][N3] = &a;
Dereferencing the pointer you will get lavlue of the array itself.
For example
sizeof( *p )
is equal to the sizeof( a[N1][N2][N3] )
.
Below is an example of more complicated declaration
int * ( * array[10] )[10];
It is a declaration of an array of 10 elements of pointers to arrays of 10 elements of pointer to the type int
.
Here is a demonstrative program that shows how to process such an array. Investigate it.:)
#include <stdio.h>
int main(void)
{
enum { N = 10 };
int a[N] =
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
};
int * b[N] =
{
&a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7], &a[8], &a[9]
};
int * ( * array[N] )[N] =
{
&b, &b, &b, &b, &b, &b, &b, &b, &b, &b
};
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < N; j++ )
{
printf( "%d ", ( **array[i] )[j] );
}
putchar( '\n' );
}
putchar( '\n' );
return 0;
}
The program output is
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9