title: Convert to std::string and get const char * in one line
body: <p>I have a number that I <strong>need</strong> to convert to a <code>const char *</code> (an API I'm using them requires <code>const char *</code> as input to many of its functions). The following works:</p>
  
  <pre><code>int num = 5;
  std::string s = std::to_string(5);
  const char * p = s.c_str();
  </code></pre>
  
  <p>as suggested by answers like those in <a href="https://stackoverflow.com/questions/10847237/how-to-convert-from-int-to-char">how to convert from int to char*?</a>, but it involves creating the seemingly unnecessary variable <code>s</code>, so I tried the following, but it doesn't work (p points to an empty string afterwards):</p>
  
  <pre><code>int num = 5;
  const char * p = std::to_string(num).c_str();
  </code></pre>
  
  <p>Is there a clean way I can accomplish this? Why doesn't the second example work? The behavior is very similar to what happens if I made this obvious mistake:</p>
  
  <pre><code>const char * p;
  {
      std::string tempStr( "hi" );
      p = tempStr.c_str( );
      // p points to "hi" string.
  }
  // now p points to "" string.
  </code></pre>
  
  <p>Which makes me suspect that the issue <code>std::to_string(num)</code> immediately goes out of scope or something similar because it's not used to directly initialize anything.</p>