【发布时间】:2013-06-25 15:50:13
【问题描述】:
要遍历我使用的字符串 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()的工作原理。特别是stackoverflow.com/questions/3889992/please-help-in-strtok?rq=1 -
*tok测试不是标准 strtok 习语的一部分。看起来它想在空字段上提前停止,但它不会起作用,因为 strtok 会跳过空字段。 -
@WumpusQ.Wumbley 所以 *tok 没用,应该删除吗?谢谢
-
它什么也没做。删除
&& *tok不会改变行为。如果要检测空字段,则不能使用strtok。
标签: c