title: Structures in C as function return value
body: <p>i would like to use <strong>strucs in plain C</strong> to manage Data on a microcontroller. I read <strong>a lot</strong> of online tutorials, but could not find the solution to my problem. The <strong>µC always runs into HardFault</strong>.</p>
  
  <p>This is the struct definition in the header file:</p>
  
  <pre><code>typedef struct{
    uint8_t laenge;
    uint8_t rfu;
    uint16_t nsi;
    uint8_t epc_data[24];
    uint16_t crc16;
  }epc_memory;
  </code></pre>
  
  <p>this is the function prototype :</p>
  
  <pre><code>epc_memory decodeACK(uint32_t rxBuffer[]);
  </code></pre>
  
  <p>The function :</p>
  
  <pre><code>epc_memory decodeACK(uint32_t rxBuffer[])
  {
  counter=0;
  epc_memory mem;
  //Anfang finden
  while(rxBuffer[counter] &gt; ERROR_) counter++;
  makeString(rxBuffer,counter);
  startEnding();
  counter = decoder();
  if(counter &gt; 100){
  
          for (counter=0; counter&lt;4; counter++)
          {
              mem.laenge = (mem.laenge &lt;&lt; 1) | decoded[counter];
          }
  
          for (counter=4;counter&lt;6; counter++)
          {
              mem.rfu = (mem.rfu &lt;&lt; 1) | decoded[counter];
          }
  
          for (counter=6;counter&lt;15; counter++)
          {
              mem.nsi = (mem.nsi &lt;&lt; 1) | decoded[counter];
          }
  
          for(counter=15;counter&lt;(15+mem.laenge);counter++)
          {
              mem.epc_data[(counter-15)/4] = (mem.epc_data[(counter-15/4)] &lt;&lt; 1) | decoded[counter];
          }
  
          for(counter=(15+mem.laenge);counter&lt;(31+mem.laenge);counter++)
          {
              mem.crc16 = (mem.crc16 &lt;&lt; 1) | decoded[counter];
          }
  
  
      }
  
  return mem;
  
   }
  </code></pre>
  
  <p>And this is how it is called in the main :</p>
  
  <pre><code>epc_memory tmp = decodeACK(&amp;rxBuffer);
  </code></pre>
  
  <p>Can you spot any errors ?</p>
  
  <p>Thanks</p>
  
  <p><strong>edit:</strong>
  just for reference...
  THIS function works just fine :</p>
  
  <pre><code>uint16_t decodeRN16(uint32_t rxBuffer[])
  {
  counter=0;
  uint16_t rn16 = 0;
  //Anfang finden
  while(rxBuffer[counter] &gt; ERROR_) counter++;
  makeString(rxBuffer,counter);
  startEnding();
  counter = decoder();
  if(counter &gt; 16){
  
      for (counter=0; counter&lt;16; counter++)
      {
          rn16 = (rn16 &lt;&lt; 1) | decoded[counter];
      }
  
  }
  
  return rn16;
  
  }
  </code></pre>