title: returning string from function without malloc
body: <p>Is it possible to return string from a function without calling <code>malloc</code>?
  I have a function as below:</p>
  
  <pre><code>char* getString(){
        char tempStr[20]
  
        // open file, read char by char and assign to tempStr
        // ....
  
        char* str = (char*)malloc(sizeof(char)*20);
        strcpy(str, tempStr); // assume this copy the null terminating char as well
        return str;
  }
  </code></pre>
  
  <p>And then when I call the <code>getString()</code>, I assign the return value to a <code>char*</code>, and then free it when I'm done, just like below:</p>
  
  <pre><code>void randomFunction(){
        char* line = NULL;
        int i = 0;
        while (i &lt; 1000) {
             line = getString();
             // do stuff with line
             free (line);  
             line = NULL;
        }
  }
  </code></pre>
  
  <p>However, I am wondering if there is any way to do this without <code>malloc</code>? And, is this the proper way of returning string from a C function?
  I tried to do some research regarding how to return without <code>malloc</code>, but didn't find clear answers. I am new to C and still learning.</p>