为了循环遍历字符串str,我使用了以下代码:
我希望能够理解这个循环的工作原理。在我看来:
for (tok = strtok(str, ";"); tok && *tok; tok = strtok(NULL, ";"))
{
//do stuff
}
我希望能够理解这个循环的工作原理。在我看来:
(1) tok = strtok(str, ";"); //initialization of tok with the first token in str
(2) tok = strtok(NULL, ";"); // go to the next token in str? how does this work?
(3) tok && *tok; //this stops the loop when tok =NULL or *tok=NULL
我很感谢您的帮助!
strtok()
如何工作的更多详细信息。特别是https://dev59.com/dm865IYBdhLWcg3wSMkw?rq=1 - Barmar*tok
测试不是标准的 strtok 习惯用法的一部分。它似乎想要在空字段上提前停止,但这不起作用,因为 strtok 跳过空字段。 - user2404501&& *tok
不会改变行为。如果你想检测空字段,就不能使用 strtok。 - user2404501