1. Home
  2. Computer Science MCQs
  3. C Programming
  4. Why can strtok not be used to tokenise two different…

Added 30 Nov -0001

Why can strtok not be used to tokenise two different strings at the same time in one thread?

  1. It is not reentrant because it calls malloc internally on the first call
  2. It keeps the scan position in static storage shared between all calls Correct answer
  3. It writes into the source string, so two sources would overwrite each other
  4. It returns pointers into a single static buffer reused on every call

Correct answer

B. It keeps the scan position in static storage shared between all calls

18 views

MCQSEXAM

Explanation

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.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is B? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs