title: If referencing constant character strings with pointers, is memory permanently occupied?
body: <p>I'm trying to understand where things are stored in memory (stack/heap, are there others?) when running a c program. Compiling this gives warning: function return adress of local variable:</p>
  
  <pre><code>char *giveString (void)
  {
      char string[] = "Test";
      return string;
  }
  int main (void)
  {
      char *string = giveString ();
      printf ("%s\n", string);
  }
  </code></pre>
  
  <p>Running gives various results, it just prints jibberish. I gather from this that the char array called string in giveString() is stored in the stack frame of the giveString() function while it is running. But if I change the type of string in giveString() from char array to char pointer:</p>
  
  <pre><code>char *string = "Test";
  </code></pre>
  
  <p>I get no warnings, and the program prints out "Test". So does this mean that the character string "Test" is now located on the heap? It certainly doesn't seem to be in the stack frame of giveString() anymore. What exactly is going on in each of these two cases? And if this character string is located on the heap, so all parts of the program can access it through a pointer, will it never be deallocated before the program terminates? Or would the memory space be freed up if there was no pointers pointing to it, like if I hadn't returned the pointer to main? (But that is only possible with a garbage collector like in Java, right?) Is this a special case of heap allocation that is only applicable to pointers to constant character strings (hardcoded strings)?</p>