C上的Strtok实现

Strtok realization on C

我正在用 C 语言制作我自己的 Strtok 版本。

我几乎完成了,但是,我需要的最后一部分是在 Internet 上找到的,但我并不真正理解它的作用。最终我真的明白了它的作用,但仍然不明白它为什么起作用。我缺乏理论;(

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;

谢谢!

我会回答所问问题的非题外话version/part。

*c = 0;c 指向的内容设置为 0。 next = c+1; 使 next 指向 c 后一。我假设您可以发现该措辞的相似性和 strtok().

的规范