【发布时间】:2021-12-10 09:14:17
【问题描述】:
我正在 C 上制作我自己的 Strtok 版本。
我几乎完成了,但是,我在互联网上找到了我需要的最后一部分,但我不太明白它的作用。最终我实际上得到了它的作用,但仍然不明白它为什么起作用。我缺乏理论;(
char* strtok_sad(char* str, const char* delim) {
static char* next = 0;
if (str) {
next = str;
}
if (*next == 0) {
return NULL;
}
char* c = next;
while(strchr(delim,*c)) {
++c;
}
if (*c == 0) {
return NULL;
}
char* word = c;
while(strchr(delim,*c)==0) {
++c;
}
if (*c == 0) {
next = c;
return word;
}
*c = 0;
next = c+1;
return word;
}
谁能解释这部分,或者至少给我发一篇解释它的文章:
*c = 0;
next = c+1;
谢谢!
【问题讨论】:
-
@Yunnosch 再次告诉你,我明白它的作用。我缺乏理论来解释它为什么起作用
-
为什么这个
strtok函数很悲伤?可能是因为您没有按照老师的要求写自己,而是从互联网上拿了一个。