title: How are C structs with arrays returned?
body: <p>Suppose we have: </p>
  
  <pre><code>typedef struct {
      uint8_t someVal;
  } Entry
  
  typedef struct {
      Entry grid[3][3];
  } Matrix
  
  //Make a 3x3 matrix of all 0s
  Matrix emptyMatrix(void) {
      Entry zero = {.value = 0}
      Matrix matrix;
      for (int i = 0; i &lt; 3; i++)
          for (int j = 0; j &lt; 3; j++) {
              //Shallow copy empty struct
              matrix.grid[i][j] = zero;
          }
      return matrix;
  }
  
  Matrix myMatrix = emptyMatrix();
  </code></pre>
  
  <p>I understand that in C we're <a href="https://stackoverflow.com/questions/9653072/return-a-struct-from-a-function-in-c">allowed to return a struct</a> from a function so this works and I've tested it. However, it's unclear to me HOW the assignment works. </p>
  
  <p>Does the compiler allocate the memory for <code>myMatrix</code> then copy each <code>Entry</code> element of the array in the <code>Matrix</code> struct returned by <code>emptyMatrix()</code>?</p>
  
  <p>I guess it would also be helpful to know the memory map of <code>Matrix</code> - I assumed that since <code>grid</code> is an array that <code>Matrix</code>'s memory would contain pointers. However, it apparently stores the <a href="https://stackoverflow.com/questions/29798894/memory-layout-of-c-struct-with-arrays">value</a>. If this is the case, my guess for how the assignment works makes much more sense to me. </p>
  
  <p>Edit: It seems like people are answering the question incompletely. I want to know whether my guess of how the assignment works is correct. </p>