char *pos = strchr(buf + 2, '\0');检测NULL,不是返回空吗
strchr函数当查找失败时,返回NULL。
当查找成功时,返回一个指针,该指针指向找到的字符。
题目中的代码,查找字符串结束字符'\0',是能够成功的,所以并不返回NULL,
而是返回指向'\0'字符的指针。
看一下例子:
#include <stdio.h>
#include <string.h>
int main() {
char buf[] = "hello"; //字符串末尾隐含'\0'
char *pos = strchr(buf + 2, '\0');
printf("%p\n",pos); //打印该指针值
printf("%d\n",*pos); //注意和上一行的区别
return 0;
}
可以运行此程序,观察输出。
如果对您有帮助,请采纳答案好吗,谢谢!