C++ strtok() - 为什么必须传递 NULL 指针才能获得字符串中的下一个标记?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29638598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 21:13:02  来源:igfitidea点击:

strtok() - Why you have to pass the NULL pointer in order to get the next token in the string?

c++cstringpointersstrtok

提问by phez1

This is the explanation of strtok().

这是strtok()的解释。

#include <string.h>
char* strtok( char* s1, 
              const char* s2 );*

The first call to strtok() returns a pointer to the first token in the string pointed to by s1. Subsequent calls to strtok() must pass a NULL pointer as the first argument, in order to get the next token in the string.

第一次调用 strtok() 返回一个指向 s1 指向的字符串中第一个标记的指针。对 strtok() 的后续调用必须传递一个 NULL 指针作为第一个参数,以便获取字符串中的下一个标记。

But I don't know, why you have to pass the NULL pointer in order to get the next token in the string. I searched about 15 minutes, but didn't find an explanation in the internet.

但我不知道,为什么必须传递 NULL 指针才能获取字符串中的下一个标记。我搜索了大约 15 分钟,但没有在互联网上找到解释。

回答by mcleod_ideafix

strtok()keeps some data inside of itself by using static variables. This way, strtok()can continue searching from the point it left off at during the previous call. To signal strtok()that you want to keep searching the same string, you pass a NULLpointer as its first argument. strtok()checks whether the first argument is NULLand if it is, it uses its currently stored data. If the first parameter is not null, it is treated as a new search and all internal data is reset.

strtok()通过使用静态变量将一些数据保存在自身内部。这样,strtok()可以从它在上次调用期间停止的点继续搜索。为了strtok()表明你想继续搜索同一个字符串,你传递一个NULL指针作为它的第一个参数。strtok()检查第一个参数是否是NULL,如果是,则使用其当前存储的数据。如果第一个参数不为空,则将其视为新搜索并重置所有内部数据。

Maybe the best thing you can do is to search for an actual implementation of the strtok()function. I've found one small enough to post it here, so you get an idea of how to handle this NULL parameter:

也许你能做的最好的事情就是搜索strtok()函数的实际实现。我找到了一个小到可以把它贴在这里,这样你就知道如何处理这个 NULL 参数了:

/* Copyright (c) Microsoft Corporation. All rights reserved. */

#include <string.h>

/* ISO/IEC 9899 7.11.5.8 strtok. DEPRECATED.
 * Split string into tokens, and return one at a time while retaining state
 * internally.
 *
 * WARNING: Only one set of state is held and this means that the
 * WARNING: function is not thread-safe nor safe for multiple uses within
 * WARNING: one thread.
 *
 * NOTE: No library may call this function.
 */

char * __cdecl strtok(char *s1, const char *delimit)
{
    static char *lastToken = NULL; /* UNSAFE SHARED STATE! */
    char *tmp;

    /* Skip leading delimiters if new string. */
    if ( s1 == NULL ) {
        s1 = lastToken;
        if (s1 == NULL)         /* End of story? */
            return NULL;
    } else {
        s1 += strspn(s1, delimit);
    }

    /* Find end of segment */
    tmp = strpbrk(s1, delimit);
    if (tmp) {
        /* Found another delimiter, split string and save state. */
        *tmp = '##代码##';
        lastToken = tmp + 1;
    } else {
        /* Last segment, remember that. */
        lastToken = NULL;
    }

    return s1;
}

回答by StilesCrisis

If you pass a non-NULL value, you are asking it to start tokenizing a different string.

如果你传递一个非 NULL 值,你是在要求它开始标记一个不同的字符串。

If you pass a NULL value, you are asking to continue tokenizing the same thing as before.

如果您传递 NULL 值,则表示您要求继续标记与以前相同的内容。