简体   繁体   English

C上的Strtok实现

[英]Strtok realization on C

I am making my own version of Strtok on C.我正在 C 上制作自己的 Strtok 版本。

I almost completed, however, the last part I needed I found on the internet and I don't really understand what it does.我几乎完成了,然而,我需要的最后一部分是在互联网上找到的,我真的不明白它的作用。 Eventually I actually got what it does but still don't understand why it works.最终我实际上得到了它的作用,但仍然不明白它为什么起作用。 I am lacking of theory;(我缺乏理论;(

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;
}

Can somebody explain this part or at least send me an article where it is explained:有人可以解释这部分,或者至少给我发一篇解释它的文章:

    *c = 0;
    next = c+1;

Thanks!谢谢!

I will answer the non-offtopic version/part of the asked question.我将回答所问问题的非题外话版本/部分。

*c = 0; sets to 0 whatever c points to.无论c指向什么,都设置为 0。 next = c+1; makes next to point one behind c .c后面的第一个点next I assume that you can spot the similarity of that rephrasing and the specification of strtok() .我假设您可以发现该改写和strtok()规范的相似之处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM