Disgusting C language strtok Function

Source: Internet
Author: User
Tags strtok
From C #, Java to C, I feel like I have returned to the primitive society from a public-productive society, so I am very angry !!!

Function Name: strtok
Function: searches for words separated by the delimiters specified in the second string.
Usage: Char   * Strtok ( Char   * Str1, Char   * Str2 );
Program example:

# Include < String . H >  
# Include < Stdio. h >  

Int Main ( Void )
{
Char Input [ 16 ] =   " ABC, d " ;
Char   * P;

/**/ /*Strtok places a null Terminator
In front of the token, if found*/  
P = Strtok (input, " , " );
If (P) printf ( " % S \ n " , P );

/**/ /*A second call to strtok using a null
As the first parameter returns a pointer
To the character following the token*/  
P = Strtok (null, " , " );
If (P) printf ( " % S \ n " , P );
Return   0 ;
}  

The following is a disgusting strtok function used to separate strings.
Its second parameter cannot be a char * variable. It is OK if it is a char [] variable.
Fuck!
I did n tests to find this problem, which caused me to debug UNIX Network Programming jobs for a long time.

This function is really strange to call.
The parameter needs to be set for the first time. The second parameter can be null.
Then the function can actually split the characters set for the first time.
I'm speechless
This design is too unfriendly.

Let's take this function seriously. If someone who is as depressed as me finds this functionArticle, Maybe less detours
Two parameters must be set for the first call of the function. The strtok (STR, ",") STR must be separated by the string ",".
Returns the first, previous, or preceding string in the string.ProgramFirst output ABC

The second call to this function strtok (null, "."), the first parameter is set to null, and the second parameter is based on segmentation.
The result returns the Split Based on the subsequent string, that is, the above program outputs d

Related Article

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.