Do not understand this while loop

closed account (EwCjE3v7)
What does this mean, I`m so confused
EXCER:
What does the following program do?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include <iostream>
#include <cstring>
#include <vector>
using std::cout; using std::endl;
using std::vector;

int main()
{
    const char ca[]= {'h', 'e', 'l', 'l', 'o'};
    const char *cp = ca;
    while (*cp) { // wth dosn`t this mean dereference the pointer?
        cout << *cp << endl;
        ++cp;
    }
    return 0;
}
The program has indeterminate output.

ca is an array of 5 strings without a null terminator.

cp is a pointer to ca[0].

*cp is ca[0]

while (*cp) is supposed to loop until *cp hits a null terminator, but remember ca is not null terminated, so the loop will just keep going until it hits some other null somewhere.
Last edited on
closed account (EwCjE3v7)
Thank you kbw, I was right after all, :D well thats what I thought. When I ran it like around 2-3 times it gave me 3 of the same symbols after the string, and I thought it would be different(undefined)
Topic archived. No new replies allowed.