title: returning a local variable from function in C
body: <pre><code>#include &lt;stdio.h&gt;
  
  int foo1(void)
  {
      int p;
      p = 99;
      return p;
  }
  
  char *foo2(void)
  {
      char buffer[] = "test_123";
      return buffer;
  }
  
  int *foo3(void)
  {
      int t[3] = {1,2,3};
      return t;
  }
  
  int main(void)
  {
      int *p;
      char *s;
  
      printf("foo1: %d\n", foo1());
      printf("foo2: %s\n", foo2());
      printf("foo3: %d, %d, %d\n", p[0], p[1], p[2]);
      return 0;
  }
  </code></pre>
  
  <p>When I compile this with <code>gcc -ansi -pedantic -W -Wall</code> the compiler issues warning messages for foo2() and foo3():</p>
  
  <pre><code>warning: function returns address of local variable
  </code></pre>
  
  <p>I thought it is not allowed to return a local variable, but foo1() works fine and it seems there is a huge difference between returning pointer to a local object and the object itself.</p>
  
  <p>Could anybody shed some light on this issue? Thanks in advance!</p>