我正在學習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。 –