"C language" talking about strtok () and null

Source: Internet
Author: User
Tags first string strtok truncated

One, strtok () function

The Strtok () function is included in the header file String.h

Syntax: Char *strtok (char *str1, const char *STR2);

Function: The function returns a pointer to the part of the string str1 that is immediately marked, and the string str2 is the delimiter that is marked . If the delimiter tag is not found, the function returns NULL. To convert a string to a token, the first call to str1 points to the delimiter as a token. After that, all calls to STR1 should be null


Second, the Code

#include <stdio.h> #include <string.h>int main () {char p[]= "My name is xiaoming."; Char Tmp[20];char *sep= ""; char *token=null;strcpy (TMP,P); for (Token=strtok (TMP,SEP); Token! = Null;token=strtok (NULL, Sep) {printf ("%s\n", token);} return 0;}



Iii. Results of operation




Iv. talking about the Strtok () function and null

the function strtok (char *str1, const char *STR2) involves two pointers.

The first pointer, Pointer_a, is used to point to the string returned by the function, which is the first string that is truncated by the character in the original string originalstring by the characters in the SEPs.

The second pointer pointer_b is used to point to originalstring, matching the position of the truncated string seps.

if, in the second function call, the first argument is not set to NULL but is set to the original string "My name is xiaoming." , then always return to "My" and will also fall into a dead loop.

Why did this result come about?

The reasons are:

The first call to this function is outside the while loop body (line 19th), at which point the string "My name is xiaoming." Matches the token (that is, the various characters represented by the parameter SEPs, as in this example, ",/t/n" means space, comma, tab, newline character, four characters) The position of the character specified in is between my and name. The return value of the function is then pointer_a to a pointer to "My". At this point, pointer_b points to the location between my and name, which indicates that the position before Pointer_b has been found to match.

Thus, when a second function call is made within the loop body, the first parameter of Strtok () is set to NULL, and the lookup match can be started directly from the Pointer_b position.

Therefore, NULL is only intended to make every invocation, not from "My name is xiaoming." 's head, but starts from the location where the last call was stopped, so it loops until it is no longer possible to find the position that matches the token, thus implementing the "my name is Xiaoming." The effect of separating according to token.


"C language" talking about strtok () and null

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.