How does strtok() split the string into tokens in C?

前端 未结 15 2033
陌清茗
陌清茗 2020-11-22 14:48

Please explain to me the working of strtok() function. The manual says it breaks the string into tokens. I am unable to understand from the manual what it actua

15条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 15:27

    strtok() stores the pointer in static variable where did you last time left off , so on its 2nd call , when we pass the null , strtok() gets the pointer from the static variable .

    If you provide the same string name , it again starts from beginning.

    Moreover strtok() is destructive i.e. it make changes to the orignal string. so make sure you always have a copy of orignal one.

    One more problem of using strtok() is that as it stores the address in static variables , in multithreaded programming calling strtok() more than once will cause an error. For this use strtok_r().

提交回复
热议问题