The strtok
function modifies its first argument. From a man page:
BUGS Be cautious when using these functions. If you do use them, note that:
* These functions modify their first argument. * These functions cannot be used on constant strings.
This means that you can pass a const char*
as the first argument as you are doing with the variable named string
.
strtok
works by inserting a NULL in the passed string at every delimeter which allows it to return each token without allocating memory for the returned string. I doubt whether SDCC supports the reentrant version strtok_r
but it would be possible to use that instead if it existed.
Alternatively, create your own copy of the incoming string before parsing. If you know the maximum incoming string size then the string copy can be allocated on the stack at the top of your parsing function.