title: How to split a C-String into an array of C-Strings
body: <p>In C++, I want to do</p>
  
  <pre><code>char buffer[1024] = "command insert file1 file2 ..."
  </code></pre>
  
  <p>and turn it into</p>
  
  <pre><code>*argv[0] = "command"
  *argv[1] = "insert"
  *argv[2] = "file1"
  </code></pre>
  
  <p>et cetera. Isn't there some simple way to do this, like split() or whatever? I can't use boost or vectors because the elements need to be c-strings for linux library functions (like execvp), and they need to compile on a server without boost or any extra libraries</p>
  
  <p>I've seen examples online that use strtok, but the examples don't seem to store the values afterward, just print them. The other way seems to be some ridiculously complicated method that involves looping through and counting spaces and escape characters.
  Isn't there an easier way? This is driving me nuts.</p>
  
  <p>EDIT: As a point of reference, this is what my main function looks like so far:</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;cstdlib&gt;
  #include &lt;cstdio&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  void runCommand(char **argv);
  void splitIntoArgs(char *command, char **argv);
  
  int main()
  {
  
     char buffer[1024];
     char *argv[5];
  
     while (1)
     {
         // prompt user for command to run
         cout &lt;&lt; "Enter command: ";
         cin &gt;&gt; buffer; //read the buffer
         cout &lt;&lt; endl;
         splitIntoArgs(buffer, argv); //split the command into separate arguments
         if (strcmp(argv[0], "exit") == 0)
             return 0;
         runCommand(argv);
     }
  
  }
  </code></pre>