#include
#include
int main()
{
char *res1 = strstr("xxxhost: www.baidu.com", "host");
if(res1 == NULL)
printf("res1 is NULL!\n");
else
printf("%s\n", res1);
const char *res2 = strstr("xxxhost: www.baidu.com", "cookie");//疑问
if(res2 == NULL)
printf("res2 is NULL!\n");
else
printf("%s\n", res2);
return 0;
}
//运行结果是
host: www.baidu.com
res2 is NULL!
但是为什么,我将const char res2 改成char *res2编译就报错。
error: invalid conversion from 'const char' to 'char*' [-fpermissive],
第一个不也是正常吗?实在没想明白
推荐阅读:https://blog.csdn.net/huan447882949/article/details/82852423
个人觉得不能去掉const标签,比较字符串应该不能改变。
然后就是,strstr函数是找相同的子串,“cookie”字串在"xxxhost: www.baidu.com"字串中没有出现。
希望采纳,谢谢