Why can strtok not be used to tokenise two different strings at the same time in one thread?
Correct answer
B. It keeps the scan position in static storage shared between all calls
18 views
B. It keeps the scan position in static storage shared between all calls
18 views
strtok stores the position for the next call in an internal static pointer, so interleaving calls for two strings corrupts the scan state; passing NULL continues the previous string. The reentrant replacement strtok_r takes a caller-owned save pointer. strtok does modify the source by writing NUL over each delimiter, but the static scan state is the reason it is non-reentrant, not the buffer it returns.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is B? Ask here and get an explanation.