title: c++ std::string array as function parameter
body: <p>I'm kind of new to C++. I want to make split function for std::string in c++ like java split function in String class(I don't want to use boost library). so I made custom split function which is..</p>
  
  <pre><code>using namespace std;
  
  void ofApp::split(string ori, string tokens[], string deli){
      // calculate the number of tokens
      int length = 0;
      int curPos = 0;
  
      do{
          curPos = ori.find(deli, curPos) + 1;
          length++;
      }while(ori.find(deli, curPos) != string::npos);
      length++;
  
      // to save tokens, initialize tokens array
      tokens = new string[length];   // this is the line I'm suspicious about..
  
      int startPos = 0;
      int strLength = 0;
      int curIndex = 0;
      do{
          strLength = ori.find(deli, startPos) - startPos;
          tokens[curIndex++] = ori.substr(startPos, strLength);
          startPos = ori.find(deli, startPos) + 1;
      }while(ori.find(deli, startPos) != string::npos);
      tokens[curIndex] = ori.substr(startPos, ori.length() - startPos);
  }
  </code></pre>
  
  <p>First, I thought passing parameter as <strong>string tokens[]</strong> is the way call by reference, so when function is finished, tokens[] array will be full of tokens seperated by deli string. But when i call this function like</p>
  
  <pre><code>string str = "abc,def,10.2,dadd,adsf";
  string* tokens;
  split(str, tokens, ",");
  </code></pre>
  
  <p>after this, tokens array is completely empty. On my guess, this happens because of the line</p>
  
  <pre><code>tokens = new string[length];
  </code></pre>
  
  <p>I think memory space for tokens array as local variable is allocated and when split function is finished, this memory space will be free as block is finished.</p>
  
  <p>when i try to debug, split function itself is working very well as tokens array is full of tokens at least in split function block. I think my guess is right but how can I solve this problem? Any solution? I think this is not only matter of std::string array, this is homework of "call by reference". </p>
  
  <p><strong>Requirement</strong></p>
  
  <ol>
  <li>pass std::string[] type to function parameter (return tokens[] is OK too. But I think this will have same problem)</li>
  <li>when function is finished, array must full of tokens</li>
  <li>tokens array length must be calculated in split function(if user has to calculate tokens length, it is foolish function). Because of this, memory for tokens array can't be allocated before split function call.</li>
  </ol>
  
  <p>Thank you in advanced for your great answer!</p>