gpt4 book ai didi

c - strtok() 问题 : If tokens are delimited by delimiters, 为什么最后一个标记位于分隔符和空值 '\0' 之间?

转载 作者:太空狗 更新时间:2023-10-29 17:06:40 28 4
gpt4 key购买 nike

在下面的程序中,strtok() 在主要部分按预期工作,但我无法理解一个发现背后的原因。我读过有关 strtok() 的内容:

To determine the beginning and the end of a token, the function first scans from the starting location for the first character not contained in delimiters (which becomes the beginning of the token). And then scans starting from this beginning of the token for the first character contained in delimiters, which becomes the end of the token.

来源:http://www.cplusplus.com/reference/cstring/strtok/

正如我们所知,strtok() 在每个标记的末尾放置一个 \0。但是在下面的程序中,最后一个定界符是一个点(.),之后在那个点和引号之间有一个Toad(") >). 现在点在我的程序中是分隔符,但是Toad 后面没有分隔符,甚至连空格都没有(在我的程序中是分隔符)。请清除以下引起的混淆这个前提:

为什么 strtok()Toad 视为标记,即使它不在 2 个分隔符之间?这是我在遇到 NULL 字符 (\0) 时读到的关于 strtok() 的内容:

Once the terminating null character of str has been found in a call to strtok, all subsequent calls to this function with a null pointer as the first argument return a null pointer.

来源:http://www.cplusplus.com/reference/cstring/strtok/

它没有说一旦遇到空字符,就会返回一个指向 token 开头的指针(我们这里甚至没有 token ,因为我们没有得到 token 的结尾,因为没有从 token 开头(即从 Toad 的“T”)开始扫描后发现的分隔符,我们只发现了一个空字符,不是分隔符)。那么,为什么 strtok()参数字符串的最后一个定界符和引号之间的部分 视为标记?请解释一下。

代码:

#include <stdio.h>
#include <string.h>

int main ()
{
char str[] =" Falcon,eagle-hawk..;buzzard,gull..pigeon sparrow,hen;owl.Toad";
char * pch=strtok(str," ;,.-");

while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ;,.-");
}

return 0;
}

输出:

Falcon
eagle
hawk
buzzard
gull
pigeon
sparrow
hen
owl
Toad

最佳答案

strtok (7.24.5.8) 的标准规范非常清楚。特别是第 4 段(强调由我添加)与问题直接相关,如果我理解正确的话:

3 The first call in the sequence searches the string pointed to by s1 for the first character that is not contained in the current separator string pointed to by s2. If no such character is found, then there are no tokens in the string pointed to by s1 and the strtok function returns a null pointer. If such a character is found, it is the start of the first token.

4 The strtok function then searches from there for a character that is contained in the current separator string. If no such character is found, the current token extends to the end of the string pointed to by s1, and subsequent searches for a token will return a null pointer. If such a character is found, it is overwritten by a null character, which terminates the current token. The strtok function saves a pointer to the following character, from which the next search for a token will start.

通话中

char *where = strtok(string_or_NULL, delimiters);

返回的标记(指向它的指针)——如果有的话——从从起始位置(包括在内)找到的第一个非定界符延伸到下一个定界符(不包括),如果存在的话,或者结束字符串,如果后面的分隔符不存在的话。

链接的描述没有明确提到 token 延伸到字符串末尾的情况,这与标准相反,因此在这方面它是不完整的。

关于c - strtok() 问题 : If tokens are delimited by delimiters, 为什么最后一个标记位于分隔符和空值 '\0' 之间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16571060/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com