私のシステムでは、次のプログラム:
int main(){
char *strgptr;
char buf[5] = {'b','a','a','a','\0'};
char *tmp = strtok_r(buf, ".", &strgptr);
if(tmp != NULL){
printf("Found a . in baaa?\n");
printf("It was found starting at: %s\n", tmp);
}
else
printf("Everything is working.\n");
}
プリント:
Found a . in baaa?
It was found starting at: baaa
ただし、「。」を交換すると。「a」のstrtok_rの区切り文字列、私は(予想どおり)取得します:
Found a . in baaa?
It was found starting at: b
ただし、「。」を交換します。buf に表示されないその他の文字 (例: "c") の場合:
Found a . in baaa?
It was found starting at: baaa
strtok_r の man ページには、予想どおり、次のように書かれています。
The strtok() and strtok_r() functions return a pointer to the next token,
or NULL if there are no more tokens.
では、問題のトークンをまったく含まない文字列が渡されたときに strtok_r が NULL を返さないのはなぜでしょうか?