The code has comments which are inaccurate or misleading. Corrected below:
1 2 3 4 5
|
char s[] = "hello"; // s here means array of characters
for (char *cp = s; *cp != 0; cp++) // cp here means pointer to a character
{
printf("char is %c \n", *cp);
}
|
The initial contents of the array s is
{ 'h', 'e', 'l', 'l', 'o', '\0'}
That is the array is 6 characters in length, the first element is the letter 'h' and the last element is zero, sometimes represented as the character '\0'.
If we interpret the array as a character string, then the zero marks the end of the string.
The for loop uses the pointer
char * cp
which is set to point to the first element of the array, which is the letter 'h'.
The printf statement uses the
%c
code which is used to print the single character pointed to by cp. The loop increments the pointer cp to step through the array (or string) one character at a time. When the value pointed to is equal to 0 the loop terminates.