Case 1:
printf("first character is %c", *my_pointer);
You are telling
printf
to output a single character (
%c
) so the parameter after the format string must be a single character. Since the character is inside an array pointed to by
my_pointer
it needs to be extracted from the array, and the way to do that is to use the dereferencing operator (
*
), which will return the single character from the location pointed to.
Case 2:
printf("%s \n",my_pointer);
In this case you are telling
printf
to output a sequence of characters (
%s
) so you must pass the address of that sequence. And since my_pointer points to the start of the array of characters, that is what it will do. Internally printf will do something like:
int printf(char* format, char* string)
{
while (*string != '\0') {
char c = *string; ++string; }
}