When the mystrtok function is called the second time, we want the input pointer to point at the first word’s ending, but every time the function is called we are initialising input to NULL (in the line static char* input=NULL) so shouldn’t this make the input pointer point to NULL everytime mystrtok is called? How does the output come?
How does the code work if input is initialised to NULL every time mystrtok is called?
@aditics99
Since the strtok function uses a static variable , this is easy to implement. The static variable remembers the location of the last index of the previous input string. If we provide a NULL argument to the strtok function , the function simply assigns the current pointer to the value stored in static variable. If however a different pointer value is provided to it , the function reassigns the current pointer to the new provided value.
This implementation is only possible due to the static member.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.