strtokfunction使用静态变量将字符串解析为标记.因此,当多次调用完成时,这会导致冲突.除了使用线程之外,我该如何执行以下操作:thx
- 我可以使用函数指针在2个不同的位置分配函数吗?这会使"strtok"中的静态变量分配到2个不同的位置吗?
//breaking up first by Sentence and than by Word.
char phrase[] = "My dog has fleas.\nAnd he gave them to me.";
char del1[] = "\n";
char del2[] = " ";
char *token1;
char *token2;
token1 = strtok( phrase, del1);
while( token1 != NULL )
{
printf("Sentence: %s",token1);
token2 = strtok( token1, del2);
while( token2 != NULL ){
token2 = strtok( NULL, del2);
printf("WORD: %s",token2);
}
token1 = strtok( NULL, del1);
}
Run Code Online (Sandbox Code Playgroud)
使用strtok_r()(可重入版本).