The role of strtok_s in the C language is to split the words in a string
On MSDN, the parameter table:
strtok_s
Strtoken |
String containing token or tokens. |
Strdelimit |
Set of delimiter characters. |
Context |
Used to store position information between calls to strtok_s |
Locale |
Locale to use. |
The meaning of the 4 parameters:
Strtoken
This parameter is used to store the characters that need to be split or the whole string
Strdelimit
This parameter is used to store delimiters (for example:, [email protected]#$%%^&* () \ t \ n or something that distinguishes a word)
Context
This parameter is used to store the separated string.
Locale
This parameter is used to store the address used
Although there are 4 parameters, only 3 of our controllable parameters are not controllable.
Remark
Functions that are similar to this function:
wcstok_s wide-byte version of strtok_s
_mbstok_s Multi-byte version of strtok_s
=============================================================================================================== ================================
Let's take a look at the operation of this function:
In the first call to strtok_s this function will skip the beginning of the delimiter and then return a pointer to the first word in strtoken, after the word tea inserted a null to break. Multiple calls can cause this function to go wrong, and the context pointer keeps track of the string that will be read.
Follow the parameters in the following code to better understand this function:
#include <string.h>
#include <stdio.h>
Char string[] =
". A String\tof,, tokens\nand some more tokens ";
Char seps[] = "., \t\n";
char *token = NULL;
char *next_token = NULL;
int main (void)
{
printf ("tokens:\n");
Establish string and get the first token:
token = strtok_s (string, SEPs, &next_token);
While there is tokens in "string1" or "string2"
while (token! = NULL)
{
Get Next token:
if (token! = NULL)
{
printf ("%s\n", token);
token = strtok_s (NULL, SEPs, &next_token);
}
}
printf ("The rest token1:\n");
printf ("%d", token);
}
Environment: VS2013
Use F11 to step through the commissioning:
======================================================================================================
When the program finishes running 17 lines of statement value
Token value is covered by a null
The value of Next_token is covered by the rest of the characters after a null
So Token!=null
Conforms to the condition of entering while statement,
When a program enters the Whlie statement when it finishes running 24 lines
Token value is overwritten with a string
Next_token value is overwritten with string
After several cycles
The value in token becomes NULL
When the value in the Next_token is null, it is removed by the function (plus the double quotation marks)//tips: When assigning a value to an array, the double quotation marks are initialized, and the initialization adds an additional to the end so that when an array is initialized, it takes one byte, and the curly braces are not one byte.
A small exploration of strtok_s function in C