title: How to initialize already defined flexible length array in C?
body: <p>Consider the code:</p>
  <pre class="lang-c prettyprint-override"><code>#include &lt;stdio.h&gt;
  
  int a[] = { 1, 2, 3 };
  int b[];
  
  int main() {
      b = { 4, 5, 6 };
      return 0;
  }
  </code></pre>
  <p>This of course is illegal.</p>
  <pre><code>main.c:7:9: error: expected expression before '{' token
      7 |     b = { 4, 5, 6 };
        |         ^
  </code></pre>
  <p>I need to initialize array <code>b</code> in a function. How to do that?</p>
  <p>I know one way, but it's... IDK, really?</p>
  <pre class="lang-c prettyprint-override"><code>#include &lt;stdio.h&gt;
  
  int a[] = { 1, 2, 3 };
  int b[3];
  
  int main() {
      b[0] = 4;
      b[1] = 5;
      b[2] = 6;
      return 0;
  }
  </code></pre>
  <p>Is it the only way of doing that?</p>
  <p><strong>EDIT:</strong>
  It's very hard to explain my intent.</p>
  <p>Let's say I define items in code. Right? Is that clear? In code, means - it's static, not dynamic. Not runtime. Not &quot;variable length&quot;. Let's say it will be 5 items, right? So why don't I just say 5 and set the fixed length to 5?</p>
  <p>The reason for this is, I don't have to write:</p>
  <pre class="lang-c prettyprint-override"><code>int a[5] = { 1, 2, 3, 4, 5 };
  ```c
  but instead I can write
  ```c
  int a[] = { 1, 2, 3, 4, 5 };
  </code></pre>
  <p>That saves me the redundant step of counting my items manually, because the compiler counts the items for me. It also allocates the memory on stack for them for me. No need to call <code>malloc</code>.</p>
  <p>And here goes the hard to explain part:</p>
  <ol>
  <li>Imagine instead of 1, 2, 3, 4 I have structures. They are not numbered, they contain various data types.</li>
  <li>Imagine, that later in time I add an additional item to my array, because it will become necessary at a certain point of the project development. To be absolutely clear: this is not by any means dynamic. I will never need a single item more during the runtime. It will be added to the code and committed to Git. And it will stay there, maybe to the end of the program's life.</li>
  <li>Imagine I add the item, but I forget to update the items count. It's a super common bug.</li>
  <li>The whole point for me is to learn, whether a syntax in C exists allowing to omit the item count by initializing array inside a function. Now I suspect - it is not possible. I can either make such initialization when defining a variable, but not when assigning to the variable, even if it's the only assignment made to that variable.</li>
  <li>I'd avoid to allocate more bytes than necessary, also performing more operations than necessary because it's for embedded and I really aim to cut that kind of costs. I also learn how to do the things the correct way, even if I technically could waste some resources.</li>
  </ol>
  <p>Additional problem with my real code is some data for the items is not available before a function is called.</p>
  <p>So my temporary solution is just to specify the array length. And then remember to update it when I add or remove items DURING DEVELOPMENT. That should work. If it's the correct way of doing that, than it's fine and I'm done. If not, I'd be happy to learn how to do it a better way.</p>