How does one know that pmessage is a pointer to a character array, and not a pointer to a single character?
You don't. At least, there's no way to tell from the pointer value itself whether it points to a single char
or the first element of an array of char
. It can be used either way.
You have to rely on context or specify explicitly how the pointer is to be used. For example, scanf
uses different conversion specifiers in order to determine whether a pointer is pointing to a single char
:
char single_char;
scanf( " %c", &single_char );
or an array of char
:
char array_of_char[N];
scanf( "%s", &array_of_char[0] );
Remember that when it isn't the operand of the sizeof
or unary &
operators or a string literal being used to initialize another array in a declaration, an expression of type "N-element array of T
" will be converted ("decay") to an expression of "pointer to T
", and the value of the expression will be the address of the first element of the array, so that last line could also be written
scanf( "%s", array_of_char );
Because of that conversion rule, anytime you pass an array expression to a function, what the function actually receives is a pointer value. In fact, the function declarations
void foo( char str[N] );
and
void foo( char str[] );
are equivalent to
void foo( char *str );
All three treat str
as a pointer to char
.