Bad call to strtok_r?

Hi there,

The following code is using strtok and strtok_r to split a string containing pairs of keys and values and store the result in a predefined multidimensional char global array.

All looks good when I examine the array from within the split function, correct values are in place, but when I check the array right after that in loop(), the values are not there, instead I get junk.

This symptom do no occur when I replace strtok_r with any char value so I can only assume I am doing something wrong with the call to strtok_r, but obviously it could be something else.

Thanks in advance for helping!

char * ca[5][2];
String myString = "a=red;b=green;c=blue";

void setup() {
  while (!Serial);

  // reseting array
  int i;
  for (i = 0; i < 5; i++) {
    ca[i][0] = "x";
    ca[i][1] = "nothing";

void loop() {

  //spiting the string into pairs of key=value 
  //printing outside the split function, junk in array elemnts


void splitArray(String inStr) {
  int i = inStr.length();
  char inChar[i];
  inStr.toCharArray(inChar, i);
  char * buf;
  int pos = 0;
  buf = strtok(inChar, ";");
  while(buf != NULL) {
    splitThisPair(buf, pos);
    buf = strtok(NULL, ";");
  //printing from within split unction, all looks good

void splitThisPair(char * inChar, int pos){
  char * saveptr;
  char * buf1 = strtok_r(inChar, "=", &saveptr); // command
  char * buf2 = strtok_r(NULL, "=", &saveptr); // value
  ca[pos][0] = buf1;
  ca[pos][1] = buf2;

void printArray() {
  int i;
  for (i = 0; i < 5; i++) {
    Serial.print(" -> ");
    Serial.print(" = ");

You should NOT be mixing strtok() and strtok_r() in the same program. You only have two delimiters, ; and =.

You should parse the string ONCE, getting 6 tokens - "a", "red", "b", "green", "c", and "blue".

What you do with each token is not hard to figure out.

There is NO excuse for using Strings at all. Stop doing that.

and actually (even though i sort of agree on the usage of 'String's)

int i = inStr.length();
  char inChar[i+1];
  inStr.toCharArray(inChar, i+1);

is the proper way to convert a String to a char* (creating space for the '\0')And if you want to split 'Strings' just use .subString() and .indexOf()

Thanks to both of you.