strtok() returns temporary string

Posted By: Morris

strtok() returns temporary string - 08/10/20 11:10

It appears that strtok() returns a temporary string which loses its correct value after several calls. The manual at https://manual.zorro-project.com/str_.htm points this out for other functions, but not for strtok(). Unless I am mistaken here, would you mind updating the manual?
Posted By: AndrewAMD

Re: strtok() returns temporary string - 08/10/20 12:15

Did you feed strtok a temporary string? It's better to use your own buffer.

I believe strtok actually modifies the original string that you supply, adding null terminators and pointing to a specific character in your supplied buffer to get substrings.
Posted By: jcl

Re: strtok() returns temporary string - 08/10/20 14:31

Yes, strtok modifies the string. It is a standard C function. Temporary strings are only used by specific lite-C string functions.

You can find the documentation about all standard C functions online or in a C book. It's there much more detailed than in the Zorro manual.
Posted By: Morris

Re: strtok() returns temporary string - 08/10/20 15:31

Thank you for your quick reply, gentlemen -- Andrew, yes, you are right, I did feed strtok() a temporary string. So it was the underlying temporary string that was modified outside of strtok(). All is well!
© 2024 lite-C Forums