为什么我们在 strtok() 中使用 NULL? [英] Why do we use NULL in strtok()?

查看:225
本文介绍了为什么我们在 strtok() 中使用 NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我们在strok()函数中使用NULL?

Why do we use NULL in strok() function?

while (h != NULL)
{
    h = strtok(NULL, delim);  
    if (hold != NULL) 
      printf("%s", hold);    
}

*h指向一个字符串时,这个程序会做什么?

What does this program do when *h is pointing to a string?

推荐答案

strtok 是 C 库的一部分,它的作用是将 C 以空分隔的字符串拆分为由任何分隔符分隔的标记指定.

strtok is part of the C library and what it does is splitting a C null-delimited string into tokens separated by any delimiter you specify.

对 strtok 的第一次调用必须将 C 字符串传递给标记化,随后的调用必须指定 NULL 作为第一个参数,它告诉函数继续对您首先传入的字符串进行标记化.

The first call to strtok must pass the C string to tokenize, and subsequent calls must specify NULL as the first argument, which tells the function to continue tokenizing the string you passed in first.

函数的返回值返回一个 C 字符串,它是当前检索到的标记.所以第一次调用 --> 第一个标记,第二次调用(指定为 NULL)--> 第二个标记,依此类推.

The return value of the function returns a C string that is the current token retrieved. So first call --> first token, second call (with NULL specified) --> second token, and so on.

当没有剩余的标记要检索时,strtok 返回 NULL,表示该字符串已被完全标记.

When there are no tokens left to retrieve, strtok returns NULL, meaning that the string has been fully tokenized.

这里是参考,举个例子:http://www.cplusplus.com/reference/cstring/strtok/

Here's the reference, with an example: http://www.cplusplus.com/reference/cstring/strtok/

这篇关于为什么我们在 strtok() 中使用 NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆