Question

i am getting a compiler warning, here's the code:

uint8 executeSpecialCommand(const char *string)
{
    char *parameters;           
    parameters = strtok(string, "=");

    if (parameters)
     {
         usbSendf("\nProcessing Parameters...");       
         while(parameters != NULL)
         {
             parameters = strtok(NULL, " ");
             usbSendf("\n%s", parameters);
         }
         return 1;
     }
    else
        return 0;   
}

the error points to line 3 and i think it is because "=". strtok is expecting a const char* somehow in other parts of the code i use the same and i get no warning. An ideas here to help educate me ?

UPDATE The error i get is:

warning 196: pointer target lost const qualifier

thanks

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top