title: Since I can't return a local variable, what's the best way to return a string from a C or C++ function?
body: <p>As a follow-up to <a href="https://stackoverflow.com/questions/423128/is-this-dynamic-memory-allocation">this question</a>:</p>
  
  <p>From what I've seen, this should work as expected:</p>
  
  <pre><code>void greet(){
    char c[] = "Hello";
    greetWith(c);
    return;
  }
  </code></pre>
  
  <p>but this will cause undefined behavior:</p>
  
  <pre><code>char *greet(){ 
    char c[] = "Hello";
    return c;
  }
  </code></pre>
  
  <p>If I'm right, what's the best way to fix the second greet function? In an embedded environment? On a desktop?</p>