I was trying to implement strstr in C but I was stuck at this piece of code which was crashing at runtime
while (*a==*b && a != NULL && b != NULL) {
a++
b++
}
if (b == NULL || *b == '\0') { // string found }
After googling for sometime I figured out the mistake https://stuff.mit.edu/afs/sipb/project/tcl80/src/tcl8.0/compat/strstr.c
I should've had my loop do the following:
while (*a==*b && *a != 0 && *b != 0) {
a++
b++
}
if (*b === 0) { // string found }
But I'm still not clear on why wouldn't the first approach work as well?