Why is this null termination in c++ char array not recognized

  c++

I have a very simple C++ code like so:

#include <iostream>
using namespace std;

int count_occurrences(char *pc, char c) {
    int count = 0;
    while (pc != '{$content}') {
        if (*pc == c) {
            ++count;
        }
        ++pc;
    }
    return count;
}

int main() {
    char v[6] {'h', 'e', 'l', 'l', 'o', '{$content}'}; 
    cout << "Total occurence of  'a'" << count_occurrences(&v[0], 'l');
}

The char array is null terminated, however the while (pc != '{$content}') is not able to detect the null placed at the end of the character array. I have tried while (pc != NULL) and while (pc != nullptr) but no luck.

Source: Windows Questions C++

LEAVE A COMMENT