Document directory
- 1. The function is used to break down strings. The so-called decomposition means that no new strings are generated, but some operations are done on the Content pointed to by S. Therefore, the source string s has changed!
- 3. Discussion on function return values
- 4. Discussion of parameter delim (delim is a set of delim)
- 5. the string to be decomposed. The first character is the separator.
- 6. The String constant cannot be passed to the first parameter!
The use of strtok functions is a common issue. This function is very useful and controversial. The following statements may be different from some materials or your original understanding. Therefore, I try to prove this by experiment. It is necessary to explain the experiment environment. WINXP + vc6.0 is an extremely common experiment environment. Most of the source code used in this article comes from the network. I will slightly modify it as an example. Of course, I am at a limited level, and it is inevitable that there are any mistakes. If you forgive me, you may wish to do more experiments, as evidenced by experiments.
The strtok function is prototype char * strtok (char * s, char * delim). The function is "parse s into tokens separated by characters in delim. if S is null, the saved pointer in save_ptr is used as the next starting point. translated into Chinese: Act on the string S, take the characters contained in the delim as the delimiter, cut s into substrings; If, S is null, the pointer save_ptr saved by the function will be used as the starting position in the next call.
The Return Value of the function is the pointer from the split substring.
This definition is different from some domestic website statements. It is precisely these differences that many people do not have a correct understanding of strtok. It is hoped that before calling some functions, the reader should be able to read official documents (mostly in English), rather than reading documents that can be uploaded by others.
Note the following when using strtok:
1. The function is used to break down strings. The so-called decomposition means that no new strings are generated, but some operations are done on the Content pointed to by S. Therefore, the source string s has changed!
Set the source string s to Char buffer [info_max_sz] = ", Fred male 25, John male 62, Anna female 16"; filter the string delim as char * delim = "", that is, space is the delimiter.
The code will produce the following results:
First,Buffer changed. If the buffer value is printed at this time, ", Fred" will be displayed, and "male 25... 16. In fact, the strtok function finds its first appearance Location Based on the delim delimiter, that is, the space after Fred (buffer [5]), and changes it to '/0 '. The remaining positions remain unchanged. This is a good explanation of why the print buffer value can only appear ", Fred", rather than all the content in the buffer.Therefore, be cautious when using strtok to prevent the source string from being modified.
After understanding the changes in the buffer, the return value of the function is well explained. The returned value Buf is the substring before the separator (in fact, this statement is not exact, see the detailed description of the return value in "3 ).Note: According to the variable address, the Buf still refers to the source string.
The delim operator does not change.
2. If you want to continue extracting the source string s after the first time the substring is extracted, it should be followed (second and third time... N times.
The result of the first call is extracted as described above, ", Fred ". We also want to continue to use space as the boundary to extract the following male. We can see that the call after the first time passes the null value (This indicates that the function continues to store the string implicitly from the previous call. For the second call above, a this pointer is used to point to the next place of the delimiter before the first call ends, that is, the location of 'M').
,.... And so on .....
As to why null values should be assigned, you should either remember the conclusion or check the source code of strtok. At the end of this article, we will provide some introductions.
Of course, there are also some people who love to play tricks. If you don't play the cards by the way, you should see what the result will be if you do not assign a null value to the buffer. In fact, you can also think about the answer. Passing the buffer again is equivalent to finding the delim from the beginning of the string, and the buffer has been modified (only ", Fred" is visible). Therefore, the result is that delim cannot be found.
3. Discussion on function return values
As described in "1,When a substring is extracted, the return value of strtok (assuming that the returned value is assigned to the pointer BUF) is the pointer of the retrieved substring. This Pointer Points to the starting position of the substring in the source string. The next character at the end of the substring is a separator before extraction, and is modified to '/0' after extraction '.Therefore, if you print the Buf value, you can successfully output the content of the sub-string.
What value does the function return if the substring is not extracted?
We can see that the buffer does not contain the delim. The Buf value after strtok is called is
The buffer of the source string does not change because it is not found. Buf refers to the first address of the source string, and the output value is the complete value of the entire string.
When will the return value of the function be null?
"If no split string exists, null is returned.This is an ambiguous statement. If you want to understand this problem, you may need to take a look at the implementation principle of strtok. Here, we will first describe the experiment.
When strtok is called for the first time, there is no doubt that the Buf points to ", Fred ".
For the second call to strtok, because the first parameter is null, it indicates that the position of the this pointer saved by the function to continue the previous call starts to be decomposed, that is, the position of "male 25" is decomposed. After decomposition, the Buf points to "male ".
When strtok is called for the third time, the parameter is set to null. In this case, the position of the this pointer saved for the second time is decomposed, that is, the value of "25" is decomposed. Because the sub-string containing the delim separator cannot be found, the Buf points to "25 ".
In the fourth call, the parameter is still null. At this time, the this pointer saved in the third call has pointed to '/0' at the end of the string and cannot be further decomposed. Therefore, the function returns NULL, which is also mentioned in Baidu encyclopedia. "If there is no split string, the function returns NULL ."
4. Discussion of parameter delim (delim is a set of delim)
When using strtok, many people take it for granted that the function fully matches the delim separator when splitting the string. For example, if delim = "AB", the string "acdab" is used, the function extracts "ACD ". At least this is what I think when I use it for the first time. In fact, we are all wrong. I found this problem only when looking at the source code of the function, and look at the example below.
The source string is buffer, and the delim separator is comma and space. According to the general idea, we will think that after the function is called, The Buf value is "Fred, male, 25". Is the result like this?
The first call turned out to be "Fred", not what we thought.Why?
Return to the strtok function definition in the gnu c Library: "parse s into tokens separated by characters in delim ".That is to say, all characters contained in delim can be used as separators rather than strictly matching. Delim can be understood as a set of delim separators. This is very important ~
Of course, when we break down strings, we seldom use multiple separators. As a result, many people only discuss one separator when writing examples. More people mistakenly understand the role of delim when looking at the example.
5. the string to be decomposed. The first character is the separator.
When the first character is a separator, it cannot be regarded as a special case. Strings can also be correctly decomposed according to the conventional decomposition ideas.
I want to explain that strtok is faster than conventional processing in this case.
As shown in the example. The comma-separated string "Fred male 25" can be obtained with only one call, while the ',' before F is ignored. As you can see,Strtok ignores the separator starting from the starting position during the call.This can be confirmed from the source code of strtok.
6. The String constant cannot be passed to the first parameter!
All the examples in this article Save the source string as a string array variable. If you define the source string as a String constant, the program will throw an exception because the strtok function tries to modify the value of the source string.
Well, this article details the notes for using strtok. (2) I will detail some features that strtok cannot implement and introduce the strtok_r function. Finally, I will introduce the implementation of the two functions.