在我的系统上,以下程序:
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
但是,如果我交换“。” strtok_r 中“a”的分隔符字符串,我得到(如预期的那样):
Found a . in baaa?
It was found starting at: b
但是交换“。” 对于未出现在 buf 中的任何其他字符(例如“c”)产生:
Found a . in baaa?
It was found starting at: baaa
正如预期的那样,strtok_r 的手册页说:
The strtok() and strtok_r() functions return a pointer to the next token,
or NULL if there are no more tokens.
那么为什么 strtok_r 在传递一个不包含任何相关标记的字符串时无法返回 NULL 呢?