This declaration
char *names[20];
declares an array with the name names
of 20 elements of the type char *
. It would be better to write it like
char * names[20];
This declaration
char (*place)[10];
declares a pointer with the name place
that points to an array of 10 elements of the type char
.
Here is a demonstrative program that I hope makes it more clear.
#include <stdio.h>
int main(void)
{
char * names[20] =
{
"Peter", "Bob", "Mary"
};
for ( char **p = names; *p != NULL; ++p )
{
printf( "%s ", *p );
}
putchar( '\n' );
char city[10] = "Madrid";
char (*place)[10] = &city;
puts( *place );
return 0;
}
The program output is
Peter Bob Mary
Madrid
In this declaration in the above program
char * names[20] =
{
"Peter", "Bob", "Mary"
};
three elements of the array are initialized explicitly by addresses of first characters of the string literals used as initializers. All other 17 elements of the array are implicitly initialized as null pointers.
The same program can be rewritten using typedef(s).
#include <stdio.h>
typedef char * T1;
typedef char T2[10];
int main(void)
{
T1 names[20] =
{
"Peter", "Bob", "Mary"
};
for ( char **p = names; *p != NULL; ++p )
{
printf( "%s ", *p );
}
putchar( '\n' );
T2 city = "Madrid";
T2 *place = &city;
puts( *place );
return 0;
}
The program output will be the same as shown for the previous program.