title: Is it standard-compliant to initialize a 2D array of unkown size by an 1D array initializer?
body: <p>I recently came around <a href="https://stackoverflow.com/questions/62495737/initialize-2d-array-with-1d-initializer-difference-between-int-and-char-when-de#comment110524587_62495737">this question</a> where we got a 2D array definition like this one:</p>
  <pre><code>int x[][3] = { 0, 1, 2, 3, 4, 5 };
  </code></pre>
  <p>The first dimension is empty/size of the array is unknown. The array <code>z</code> is initialized by a 1D array initializer.</p>
  <p>Now, this is what the C standard says (emphasize mine):</p>
  <blockquote>
  <p>&quot;If the aggregate or union contains elements or members that are aggregates or unions, these rules apply recursively to the subaggregates or contained unions. If the initializer of a subaggregate or contained union begins with a left brace, the initializers enclosed by that brace and its matching rightbrace initialize the elements or members of the subaggregate or the contained union. <strong>Otherwise, only enough initializers from the list are taken to account for the elements or members of the subaggregate or the first member of the contained union; any remaining initializers are left to initialize the next element or member of the aggregate of which the current subaggregate or contained union is a part</strong>.</p>
  </blockquote>
  <p>Source: ISO/IEC 9899:2018 (C18), §6.7.9/20.</p>
  <p>This means it is well-defined to initialize a 2D array of known amount of elements with a 1D array initializer.</p>
  <p>Thus, f.e.:</p>
  <pre><code>int y[2][3] = { 0, 1, 2, 3, 4, 5 };
  </code></pre>
  <p>should be equivalent to:</p>
  <pre><code>int y[2][3] = { { 0, 1, 2 } , { 3, 4, 5 } };
  </code></pre>
  <hr />
  <p>What I am worry about is this:</p>
  <blockquote>
  <p>&quot;If an array of unknown size is initialized, its size is determined <em>by the largest indexed element with an <strong>explicit</strong> initializer</em>. The array type is completed at the end of its initializer list.&quot;</p>
  </blockquote>
  <p>Source: ISO/IEC 9899:2018 (C18), §6.7.9/22.</p>
  <p>It means that if the size of the array/ the amounts of elements within it is unknown, it requires that the 2D array:</p>
  <ol>
  <li>Needs to have an largest indexed element, and</li>
  <li>This element needs to have an explicit initializer.</li>
  </ol>
  <hr />
  <p>My questions:</p>
  <ul>
  <li><p>Is that provided here?</p>
  </li>
  <li><p>Is it standard-compliant to initialize a two-dimensional array of unkown size by an one-dimensional array initializer?</p>
  </li>
  </ul>
  <p>IMHO and after my actual stand of knowledge, it shouldn't. But maybe I misunderstand something here.</p>
  <hr />
  <p>I opened this question because the other question is tagged with C and C++, so it is not real language lawyer appropriate and not focuses on C, plus the question of other question is actually quite different.</p>