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 != '\0') {
if (*pc == c) {
++count;
}
++pc;
}
return count;
}
int main() {
char v[6] {'h', 'e', 'l', 'l', 'o', '\0'};
cout << "Total occurence of 'a'" << count_occurrences(&v[0], 'l');
}
The char array is null terminated, however the while (pc != '\0') 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.
while(*pc != '\0')while (*pc)'\0') with the null pointer. A pointer to the null character is not the same thing as a null pointer.