I am trying to write a small program where I basically count the string length in a function but for some reason it is not outputting anything to the command line.
#include <stdio.h>
int search (char* string, char* substring){
int length = 0;
while(substring){
substring++;
length++;
}
return length;
}
int main(int argc, const char * argv[])
{
char string1[] = "hello world";
char* string = string1;
char substring1[] = "world";
char* substring = substring1;
int a = search(string,substring);
printf("%d", a);
return 0;
}
You need to dereference it or you’ll keep looping since there’s no obvious way for
substringto become 0.That’s because you’re not looking for the
NULLpointer but rather for the NUL (\0) character.