title: Segmentation fault when returning pointers
body: <p>I recently started learning C, and an issue came up with this code:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int* add(int* a,int* b)
  {
          //a and b are pointers to integers
          int c=(*a)+(*b);
          return &amp;c;
  }
  
  int main()
  {
          int x=2,y=4;
          int* z=add(&amp;x,&amp;y); //call by reference
          printf(&quot;sum=%d\n&quot;, *z);
          return 0;
  } 
  </code></pre>
  <p>This supposedly works in windows machines, but when I compiled it, this issue came up:</p>
  <pre><code>gcc -o hello return.c
  return.c: In function ‘add’:
  return.c:8:9: warning: function returns address of local variable [-Wreturn-local-addr]
      8 |  return &amp;c;
        |  ^~
  ./hello
  Segmentation fault (core dumped)
  </code></pre>
  <p><a href="https://stackoverflow.com/questions/8184315/why-does-gcc-throw-a-warning-when-returning-a-pointer-to-a-local-variable-and-no">This post</a> describes what happened here, but it didn't happen on the windows machine in the tutorial I've been following, and my friends' windows machine can run it too.Is there a way i can emulate this behaviour on the gcc compiler?</p>
  <p>Additionally, could someone explain why the error doesn't happen in windows? The stack frame, after being destroyed,shouldn't allow that address to be accessed again from what I understand, so why wouldn't this carry over for DOS based systems?</p>