Considerations for the use of Strtok functions __ function

Source: Internet
Author: User
Tags first string function prototype strtok

Copyright NOTICE: This article is the original article, without the owner's permission to reprint.

Directory (?) [+]Considerations for the use of Strtok functions
1. Function prototype and its basic application
The Strtok function is used to decompose a string, and its prototype is:
[CPP]View Plain Copy char *strtok (char str[], const char *delim);
Where Str is the string to be decomposed, Delim is the character used in the string to decompose, and the function returns the starting position pointer of the decomposed string. The reason is decomposition, that is, did not generate a new string, but only in the source string to do some hands and feet, so that the source string has changed, so be sure to note that the source string has changed ...
First from the simplest application, look at the following example 1.
[CPP]View plain Copy//example 1 #include <string.h> void Main () {char s[] = "192.168.0.26";         Char *delim = ".";         Char *p;            printf ("%s", Strtok (S, Delim));             while (P = strtok (NULL, Delim)) printf ("%s", p);   printf ("\ n"); }
From Example 1, you can see the basic usage of the Strtok function, enter an array of strings, and then you can use it in a certain delimiter (example 1, "."). Splits a long string into a short string. It should be noted here that when a long string is split, the first argument to the Strtok function passes through the string to be split, and the second and subsequent calls to the function, the first parameter of the Strtok function should pass in NULL, This is because when the first argument is null, the function defaults to using the Strtok starting position of the last undivided string as the starting position for this split until the split ends.
This example is the most common basic application, but just knowing it is easy to overlook a lot of detail issues, and later this article will continue to discuss some noteworthy issues.
2. Use precautions 1
The first thing we need to emphasize here is that the first parameter of the Strtok function, the char str[, is changing, as we said before, and it just moves the source string, which changes the source string. In the case of the string in Example 1, "192.168.0.26" is a source string that, after the call to Strtok, ends with a string that becomes "19201680026 ", note that the red bold part is the '. ' Replaced with ' The ', this is replaced by the string end flag character, so that when printed or used, the preceding string becomes a seemingly independent string, that is, "192", "168", "0", "26", these strings are still in the source string, Just have their own string end sign ' the '.
3. Use precautions 2
In note 1 It is mentioned that the source string has changed, note also that the first parameter of the Strtok function prototype is a character array char str[], although the argument can be a pointer if passed, but if this pointer is a string constant pointer, it can cause the program to crash when it runs. Because the source string changes, and string constants cannot be changed, for example, the following example 2 is wrong.
[CPP]View plain Copy//Example 2 #include <string.h> void Main () {char *s = "192.168.0.26";       The only difference from Example 1 is that the string array becomes a string constant pointer ...         Char *delim = ".";         Char *p;            printf ("%s", Strtok (S, Delim));             while (P = strtok (NULL, Delim)) printf ("%s", p);   printf ("\ n"); }
So if the input is a string constant pointer, it is not directly passed to the first parameter of the Strtok function, you can copy the contents of the string constant pointer to a string array, and then decompose, but this method is less efficient, so it is not recommended to use, If you really can't use the Strtok function for string decomposition, you can use other functions for similar decomposition, such as using STRSTR functions, STRCHR functions, and so on.
4. Use Precautions 3
For the second argument of the Strtok function, the delimiter, note that the characters contained in Delim can be delimiters rather than strictly matched. You can interpret Delim as a collection of delimiters. This is very important. Of course, we rarely use more than one separator when decomposing strings. This also leads to a lot of people talking about only one separator when writing an example. There are more people looking at the example of the wrong understanding of the role of Delim.
5. Use precautions 4
If the first character of the string to be decomposed by the Strtok function is the delimiter, then the Strtok function ignores the first character, starting with the next delimiter. For example: ". 192.168.0.26", then the first character '. ' is ignored, and the first string that is decomposed is "192".
In short, the Strtok function is a function that looks simple but uses a lot of detail to be aware of, so it should be understood that because the Strtok function is not thread-safe, so many cases do not use this function, but the programmer implements its own string-splitting function. This ensures thread safety, however, the STRTOK_R function is defined in Linux, which is a thread-safe version of the Strtok function, and can be used for reference to other data.
If you have other opinions, please add, thank you.
Resources:
http://blog.csdn.net/helpxs/article/details/6958975

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.