title: Weird output when converting from string to const char* in c++
body: <p>Consider the MWE below,</p>
  
  <pre><code>std::string return_string() 
  {
      return "this is a string"
  }
  int main()
  {
      const char *y = return_string().c_str();
      std::string str = return_string();
      const char *x = str.c_str();
  
      std::cout &lt;&lt; return_string() &lt;&lt; std::endl; //Prints "this is a string"
      std::cout &lt;&lt; y &lt;&lt; std::endl;  // Prints Weird characters
      std::cout &lt;&lt; x &lt;&lt; std::endl;  //Prints "this is a string"
  
      std::cin.ignore();
      return 0;
  }
  </code></pre>
  
  <p>I have a function which returns a string and I need to convert it to a c-style string. Doing <code>return_string().c_str()</code> gives me weird output like
  <code>▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌p</code>. But if I store the output of a function in a string first and then convert that string to a c-style string it works. What is wrong with the first way?</p>