title: Passing pointer to char* array into function to manipulate values on Arduino
body: <p>[PLEASE CHECK FINAL EDIT BELOW FOR UPDATE]</p>
  
  <p>My C++ is a bit rusty (to say the least) and I'm having an issue trying to pass a char array into a function to manipulate the values. Example code below:</p>
  
  <pre><code>void myFunction(char* splitStrings,String stringToSetInLoop) {
    char substringPtr[stringToSetInLoop.length()];
    stringToSetInLoop.toCharArray(substringPtr, stringToSetInLoop.length());
  
    for(int i = 0; i &lt; 10; i++) {
         splitStrings[i] = *substringPtr;
     }
  }
  
  char *mySplitStrings[10];
  myFunction(*mySplitStrings,String("Repeat Me"));
  Serial.println(mySplitStrings[0]);
  </code></pre>
  
  <p>The code does not crash, but it outputs a blank line.  I suspect that I need to initialize a 2 dimensional array outside the function to pass in so that memory space is allocated.  I'm guessing that, although the substring pointer exists inside the function, the memory is destroyed, leaving the <code>char*</code> array <code>mySplitStrings[0</code>] pointing at nothing.  Also, I think I need to pass in the reference to the array memory space, not as a pointer.</p>
  
  <p>The ultimate goal here is to be able to pass a char array into a function, assign some values to it, then use those values back in the main code loop.  If there's a better way to achieve this, then please let me know.</p>
  
  <p>Thanks in advance.  Please free me from my personal pointer/reference hell!</p>
  
  <p>EDIT: Further note, this code is being run on an arduino, so the C++ is limited.</p>
  
  <p>EDIT: When I try to pass in a reference to the char* pointer, I get this error, which I'm not sure how to change the function parameters to fix: error: cannot convert <code>char* ()[10]</code> to <code>char</code> for argument 1 to <code>void myFunction(char*, String)</code>.  Can anybody please take a stab at showing me a working example?</p>
  
  <p>EDIT:</p>
  
  <p>Thanks to the responses... I now have a working static library function that splits strings passed as a char* array.  I know it's not pretty, but it does work.  Thanks you to those who contributed. Code below:</p>
  
  <pre><code>void ExplodeString::explode(char* explodeResults[], String str, String delimiter) {
      int delimiterPosition;
      int explodeResultsCounter=0;
      String subString;
      do {
          delimiterPosition = str.indexOf(delimiter);
          if(delimiterPosition != -1) {
              subString = str.substring(0,delimiterPosition);
                  char *subStringPtr[subString.length()+1];
                  subString.toCharArray(*subStringPtr, subString.length()+1);
                  explodeResults[explodeResultsCounter++] = strdup(*subStringPtr);
                  str = str.substring(delimiterPosition+1, str.length());
          } else {  // here after the last delimiter is found
              if(str.length() &gt; 0) {
                       subString = str;
                       char *subStringLastPtr[subString.length()+1];
                       subString.toCharArray(*subStringLastPtr, subString.length()+1);
                       explodeResults[explodeResultsCounter++] = strdup(*subStringLastPtr);
               }
          }
      } while (delimiterPosition &gt;=0);
  }
  </code></pre>
  
  <p>Usage:</p>
  
  <pre><code>char* explodeResults[10];
  ExplodeString::explode(explodeResults, String("cat:dog:chicken"), String(":"));
  Serial.println(explodeResults[0]);
  Serial.println(explodeResults[1]);
  Serial.println(explodeResults[2]);
  </code></pre>
  
  <p>EDIT:  Man, this is sooo much easier when you use the stdlib:</p>
  
  <pre><code>void ExplodeString::explode(std::vector&lt;std::string&gt; &amp;explodeResults, std::string str, char delimiter) {
      std::stringstream data(str);
      std::string line;
      while(std::getline(data,line,delimiter))
      {
          explodeResults.push_back(line);
      }
  }
  </code></pre>
  
  <p>Usage:</p>
  
  <pre><code>std::vector&lt;std::string&gt; commandsResult;
  char delimiter[] = ",";
  std::string _inputString = "my,string,to,parse";
  ExplodeString::explode(commandsResult, _inputString, delimiter[0]);
  </code></pre>