title: How does function ACTUALLY return struct variable in C?
body: <p>How does a function return value is clear to me, just to kick start:</p>
  
  <pre><code>int f()
  {
    int a = 2;
    return a;
  }
  </code></pre>
  
  <p>Now <code>a</code> gets the memory in stack and its life-span is within the <code>f()</code> in order to return the value it copies the value to a special register which is read by the caller as it knows that the callee have placed the value for him.
  (Since the size of return-value-holder special register size is limited that's why we cant return large objects therefore In case of advance languages when we want to return object function actually copies the address of object in heap to that special register)</p>
  
  <p>Lets come back to C for a situation when i want to return a struct variable not pointer:</p>
  
  <pre><code>struct inventory
  {
      char name[20];
      int number;
  };
  struct inventory function();
  
  int main()
  {
      struct inventory items;
      items=function();
      printf("\nam in main\n");
      printf("\n%s\t",items.name);
      printf(" %d\t",items.number); 
      getch();
      return 0;
  }
  
  struct inventory function()
  {
      struct inventory items;
      printf(" enter the item name\n ");
      scanf(" %s ",&amp;items.name );
      printf(" enter the number of items\n ");
      scanf("%d",&amp;items.number );
      return items;
  }
  </code></pre>
  
  <p>Code forked from: <a href="https://stackoverflow.com/a/22952975/962545">https://stackoverflow.com/a/22952975/962545</a></p>
  
  <p>Here is the deal,</p>
  
  <p>Lets start with main, <code>items</code> variable declared but not initialized and then function is called which return initialized structure variable which gets copied to the one in main.
  Now I am bit blurred to understand how <code>function()</code> returned struct variable <code>items</code> which is not dynamically created(technically not in heap) so this variable's life-span is within the <code>function()</code> body, also size of variable <code>item</code> can be huge enough not to fit in special register so why it worked?.(I know we can dynamically allocate item inside function and return the address but I don't want alternative, I am looking for explanation)</p>
  
  <p><strong>Question:</strong>
  Although <a href="http://ideone.com/bbA2hA" rel="noreferrer">it works</a> but how does <code>function()</code> <strong>actually returned the struct variable and get copied</strong> to <code>items</code> variable in main when it is supposed to die with <code>function()</code> return.</p>
  
  <p>I am surely missing important thing, detailed explanation would help. :)</p>
  
  <p><strong>EDIT:</strong>
  Other Answer References:</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/a/2155742/962545">https://stackoverflow.com/a/2155742/962545</a></li>
  <li>Named as <a href="http://en.wikipedia.org/wiki/Return_value_optimization" rel="noreferrer">return value optimization</a></li>
  </ol>