title: Bad value in function returning string and const char*
body: <p>I have this function in my program</p>
  
  <pre><code>const char* Graph::toChar() {
      std::string str;
      const char* toret;
      str = "";
      for (vector&lt;Edge*&gt;::iterator it = pile.begin(); it != pile.end(); it++) {
          str += (*it)-&gt;toString();
      }
      toret = str.c_str();
      return toret;
  }
  </code></pre>
  
  <p>Then I'm debugging the function everything works properly until I'm in  <em>return toret;</em> line. I press step over and the debugger is going to <em>std::string str;</em> line and all string and charater variables become <em>""</em>, so the final return of the function is <em>""</em> (nothing).</p>
  
  <p>What am I doing wrong?</p>
  
  <p>*(<em>it)->toString();</em> is working properly and when the debugger executes *toret = str.c_str();* the value in <em>toret</em> is correct.</p>
  
  <p>Thx</p>