title: C++ using execvp with string array
body: <p>I'm writing a program to execute Unix commands with any amount of arguments. I have no problem receiving input and making tokens as I use regular strings. However, execvp will only accept an array of pointers and I'm not sure how to go about converting the array of strings to this. Here's what I have:</p>
  
  <pre><code>#include &lt;cstring&gt;
  #include &lt;iostream&gt;
  #include &lt;sstream&gt;
  #include &lt;string&gt;
  #include &lt;unistd.h&gt;
  int main()
  {
  while (true)
  {
      int argc = 0;
      std::istringstream iss;
      std::string command;
      std::cout &lt;&lt; "$&gt; ";
      getline(std::cin, command);
      iss.str(command);
      for (unsigned i = 0; i &lt;= command.length(); i++)
      {
          if (command[i] == ' ' || command[i] == '\0')
          {
              argc++;
          }
      }
      std::string arr[argc+1];
      for (int i = 0; i &lt; argc; i++)
      {
          iss &gt;&gt; arr[i];
      }
      if (arr[0].compare("quit"))
      {
          break;
      }
      else
      {
          char*argv[argc+1];
          for (int i = 0; i &lt; argc; i++)
          {
              argv[i] = arr[i].c_str(); //This line is wrong
          }
          argv[argc] = NULL;
          execvp(argv[0], argv);
      }
  }
  return 0;
  }
  </code></pre>
  
  <p>I've tried various methods and can't figure out how to convert a string to a char array in the proper manner. Methods like strcpy won't work because the length of each argument will vary. Any help would be greatly appreciated.</p>