我正在学习C并正在研究函数strchr。我发现了下面的代码,它让我感到困惑。在c上对字符进行运算
/* strchr example */
#include <stdio.h>
#include <string.h>
int main(){
char str[] = "This is a sample string";
char * pch;
printf ("Looking for the 's' character in \"%s\"...\n",str);
pch=strchr(str,'s');
while (pch!=NULL) {
printf ("found at %d\n",pch-str+1);
pch=strchr(pch+1,'s');
}
return 0;
}
具有以下输出
Looking for the 's' character in "This is a sample string"...
found at 4
found at 7
found at 11
found at 18
可变PCH和阵列STR均为炭,这是为什么对它们进行运算时,它们变成整数。我感谢所有的帮助。
删除了C++标签,因为C++答案是“不要那样做”。 –
否。对指针执行算术意味着它仍然是一个指针。顺便说一句,'pch'和'str'都不是'char'。 –
@BaummitAugen噢,来吧,它不像他们使用strtok。 –