I am currently going through a "C ++ Primer". In one of the questions of the exercise, he asks:
What does the following program do?
const char ca[] = { 'h', 'e', 'l', 'l', 'o' };
const char *cp = ca;
while (*cp)
{
cout << *cp << endl;
cp++;
}
I am very glad that I understand that * cp will continue to be the last character of the ca [] array, because there is no null character in the last element of the array.
This is more for my own curiosity about what makes the while loop false. It seems like 19 characters are always displayed on my computer. 0-4 - the welcome line, 5-11 are always the same, and 12-19 change with each execution.
#include <iostream>
using namespace std;
int main( )
{
const char ca[ ] = { 'h', 'e', 'l', 'l', 'o' };
const char *cp = ca;
int count = 0;
while ( *cp )
{
cout << count << "\t" << *cp << "\t" << (int)*cp << endl;
count++;
cp++;
}
return 0;
}
Question: what causes the while-loop to become invalid? Why are 5-11 always the same character?