实现找子串查找函数(strstr)的问题
实现找子串查找函数(strstr):不能使用字符串比较 如检查wangyang 是否有 ang
const char* StrStr(const char *str1, const char *str2){
while(*str1 != '\0'){
const char *p = str1;
const char *q = str2;
const char *res = NULL;
if(*p == *q)
{
res = p;
while(*p && *q && *p++ == *q++);
if(*q == '\0')
return res;
}
str1++;
}
return NULL;
}
int main(){
const char *str1 = "wangyang";
const char *str2 = "ang";
const char *res = StrStr(str1, str2);
if(res != NULL)
printf("res=%p\n",res);
else
printf("not find\n");
}
问题1: while(*p && *q && *p++ == *q++); while当中传*p和*q什么意思,while当中传的不是条件吗,怎么可以直接传一个指针呢?
问题2:程序函数StrStr当中返回值为空,在main函数中的StrStr为什么有返回值为res?
问题3:顺便给我解释下这个程序是如何实现了这个函数的,有点不太明白?
望热心人士能够帮忙解决!!!