Serial Input Basic's usage of Strtok() unsafe?

Thanks everyone.

I have modified the parseData() function of the Serial Input Basics - updated - #3 by Robin2 tutorial to use the strtok_r function instead. The link provided in Post #4 of this thread seems to indicate that this is a thread-safe alternative to strtok. I tested it and it seems to work fine.


void parseData() {
  
  char *str;
  char *p = tempChars; //example "hello,123,3.14"
  byte counter = 0;
  
  while ((str = strtok_r(p, ",", &p)) != NULL)  // Don't use \n here it fails
    {
     switch(counter){
      case 0:
      strcpy(messageFromPC, str);
      break;
      
      case 1:
      integerFromPC = atoi(str);
      break;
      
      case 2:
      floatFromPC = atof(str);
      break;
     }

     counter++;
    }

}

I based that off a simple example of using the strtok_r() function found here: Splitting delimited strings with strtok_r - #5 by headingwest

Please let me know if there are any troubles with my code & if this modification solves the issue mentioned in post #3.

thanks

1 Like