Strtok usage, code not working [duplicate]

佐手、 提交于 2019-12-02 13:10:59

Interesting bug! You forgot function name. Notice inside while-loop body following expression:

tokens =  (NULL, ",'");
         ^ 
          'strtok' missing 

should be:

tokens = strtok(NULL, ",'");

Interesting thing is that this is not a compilation error, in fact:

tokens = (NULL, ",'");

is a valid expression which is equals to:

tokens = ",'";

(Note: NULL has no side effects)

Read: Comma Operator: ,

The comma operator , has left-to-right associativity. Two expressions separated by a comma are evaluated left to right. The left operand is always evaluated, and all side effects are completed before the right operand is evaluated.

Due to parenthesis ( ) at rhs of =, after evaluating , operator right hand operand "," is assigned to token. And because token never assigned NULL so while(tokens != NULL) never breaks, and this is the reason that you are getting "," infinitely!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!