Advertisements
Take the following snippet as an example.
char* const (*(* const bar)[5])(int)
Cannot seem to make sense of it or more so, cannot identify the initial point from where to begin making sense of it.
>Solution :
Let’s consider the declaration step by steap
char* const (*(* const bar)[5])(int);
This part
* const bar
declares a constant pointer with the name bar
that points to to an array of 5 elements of a pointer type
*(* const bar)[5]
that is pointer to the function type
char* const (int)
Here is a demonstration program.
#include <stdio.h>
char * const f1( int n )
{
printf( "f1 called with n = %d\n", n );
return NULL;
}
char *const f2( int n )
{
printf( "f2 called with n = %d\n", n );
return NULL;
}
char *const f3( int n )
{
printf( "f3 called with n = %d\n", n );
return NULL;
}
char *const f4( int n )
{
printf( "f4 called with n = %d\n", n );
return NULL;
}
char *const f5( int n )
{
printf( "f5 called with n = %d\n", n );
return NULL;
}
int main( void )
{
char *const ( *a[5] )( int ) = { f1, f2, f3, f4, f5 };
for (int i = 0; i < 5; i++)
{
a[i]( i );
}
putchar( '\n' );
char *const ( *( *const bar )[5] )( int ) = &a;
for (int i = 0; i < 5; i++)
{
( *bar )[i]( i );
}
}
The program output is
f1 called with n = 0
f2 called with n = 1
f3 called with n = 2
f4 called with n = 3
f5 called with n = 4
f1 called with n = 0
f2 called with n = 1
f3 called with n = 2
f4 called with n = 3
f5 called with n = 4
Using typedef declarations could make the declaration of the pointer much more readable.