STRTOK函数的用法

函数原型:

char *strtok( char *strToken, const char *strDelimit );

Parameters

strToken

String containing token or tokens.

strDelimit

Set of delimiter characters.

Return Value

Returns a pointer to the next token found in strToken. They return NULL when no more tokens are found. Each call modifies strToken by substituting a NULL character for each delimiter that is encountered.意思:函数返回第一个分隔符分隔的子串后,将第一参数设置为NULL,函数将返回剩下的子串。

Remarks

The strtok function finds the next token in strToken. The set of characters in strDelimitspecifies possible delimiters of the token to be found in strToken on the current call.

Security Note    These functions incur a potential threat brought about by a buffer overrun problem. Buffer overrun problems are a frequent method of system attack, resulting in an unwarranted elevation of privilege. For more information, see Avoiding Buffer Overruns.

On the first call to strtok, the function skips leading delimiters and returns a pointer to the first token in strToken, terminating the token with a null character. More tokens can be broken out of the remainder of strToken by a series of calls to strtok. Each call tostrtok modifies strToken by inserting a null character after the token returned by that call. To read the next token from strToken, call strtok with a NULL value for the strTokenargument. The NULL strToken argument causes strtok to search for the next token in the modified strToken. The strDelimit argument can take any value from one call to the next so that the set of delimiters may vary.

Note   Each function uses a static variable for parsing the string into tokens. If multiple or simultaneous calls are made to the same function, a high potential for data corruption and inaccurate results exists. Therefore, do not attempt to call the same function simultaneously for different strings and be aware of calling one of these functions from within a loop where another routine may be called that uses the same function. However, calling this function simultaneously from multiple threads does not have undesirable effects.

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



int main(int argc, const char * argv[])
{
    char str[512] = "hello,my,world";
    char *p[10] = {NULL};
    char *pstr = str;
    printf("字符串的初始地址:%p\n", pstr);
    
    p[0] = strtok(str, ",");
    printf("p[0]= %p\n", p[0]);//为什么p[0]是字符串数组的初始地址???原因:第一次调用strtok函数将字符串的首字母地址作为标记取余串,去执行strto<span style="white-space:pre">								</span>      k函数
    int i = 0;
    
    while (p[i] != NULL)
    {
        i++;                              //i=0  p[0]!=null i=1  p[1]->分隔符后面的首字母地址及m的地址
        p[i] = strtok(NULL, ",");         //i=1  p[1]!=null i=2  p[2]->w字母的地址
        printf("p[%d],%p\n", i, p[i]);    //i=2  p[2]!=null i=3  p[3]->null
                                          //i=3  p[3] =null  循环结束
        
    }
    printf("%lu\n", strlen(*p));
    
    for (int j = 0; j < i; j++)
    {
        printf("%s\n", p[j]);
    }
    
    
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值