# Initializing Array

**URL:** <https://forum.arduino.cc/t/initializing-array/1393001>\
**Category:** Programming\
**Created:** [July 1, 2025, 7:56pm UTC](https://forum.arduino.cc/t/initializing-array/1393001 "2025-07-01T19:56:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![anuragyoyo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/anuragyoyo/32/991557_2.png) [@anuragyoyo](https://forum.arduino.cc/u/anuragyoyo)\
**Post date:** [July 1, 2025, 7:56pm UTC](https://forum.arduino.cc/t/initializing-array/1393001/1 "2025-07-01T19:56:16Z")

</div>

Assume there is a variable `int size` that is mutable and changing globally. In a function if I write `int array[size]={0}` does this make the array of length `size` filled with `0` ?

And is this function valid?

```arduino
void func(int size)
{int array[size];}

```

I have read that variable allocation might not be allowed in Arduino Boards but ESP32 boards allow it. Can anyone verify where it works.

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [July 1, 2025, 8:04pm UTC](https://forum.arduino.cc/t/initializing-array/1393001/2 "2025-07-01T20:04:29Z")

</div>

> [@anuragyoyo](#):
>
> `int array[size]={0}`

Populates the first element with the value zero.

> [@anuragyoyo](#):
>
> Can anyone verify where it works.

You can, in your IDE.

> [@anuragyoyo](#):
>
> ```arduino
> void func(int size)
> {int array[size];}
> 
> ```

This `size` is local to `func()` different from a global `size` and must be passed to `func()`

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [July 1, 2025, 8:50pm UTC](https://forum.arduino.cc/t/initializing-array/1393001/3 "2025-07-01T20:50:17Z")

</div>

> [@anuragyoyo](#):
>
> `int array[size]={0}` does this make the array of length `size` filled with `0` ?

this is more or less explicitly initializing the first element of the the array to zero and the remainder to zero by default. More often this is just written `int array [size] = {};`

but its value is when initializing to non-zero value

```arduino
int array [] = { 13, 10, 9, 11, 12 };
const int ArraySz = sizeof(array)/sizeof(int);

```

this also works with arrays of structs

```arduino
struct TurnoutDefSt {
    int ServoPin;
    int TogglePin;

    int Normal;
    int Thrown;

    int swPos;
}
turnoutDef [] = {
    {10, A2, 75, 130},
    {11, A3, 80, 130}
};
const int NUM_SERVOS = sizeof(turnoutDef)/sizeof(TurnoutDefSt);

```

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [July 1, 2025, 9:14pm UTC](https://forum.arduino.cc/t/initializing-array/1393001/5 "2025-07-01T21:14:22Z")

</div>

> [@anuragyoyo](#):
>
> And is this function valid?
> 
> ```arduino
> void func(int size)
> {int array[size];}
> 
> ```

That works because the array will be constructed on the stack when the function is called.

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [July 1, 2025, 9:16pm UTC](https://forum.arduino.cc/t/initializing-array/1393001/6 "2025-07-01T21:16:04Z")

</div>

> [@anuragyoyo](#):
>
> And is this function valid?
> 
> ```arduino
> void func(int size)
> {int array[size];}
> 
> ```

This function is valid, but perfectly useless. It creates an array named `array`, that will local to that function and will be deleted just after creation.

---

<div class="post-metadata">

**Author:** ![westfw](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/westfw/32/213443_2.png) [@westfw](https://forum.arduino.cc/u/westfw)\
**Post date:** [July 1, 2025, 9:56pm UTC](https://forum.arduino.cc/t/initializing-array/1393001/7 "2025-07-01T21:56:25Z")

</div>

> [@b707](#):
>
> > [@anuragyoyo](#):
> >
> > ```arduino
> > void func(int size)
> > {int array[size];}
> > 
> > ```
> 
> This function is valid, but perfectly useless. It creates an array named `array`, that will local to that function and will be deleted just after creation.

Also, unlike a global variable, this (local) array will NOT be initialized to zeros.

---

<div class="post-metadata">

**Author:** ![6v6gt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/6v6gt/32/225488_2.png) [@6v6gt](https://forum.arduino.cc/u/6v6gt)\
**Post date:** [July 2, 2025, 4:01am UTC](https://forum.arduino.cc/t/initializing-array/1393001/8 "2025-07-02T04:01:47Z")

</div>

> [@anuragyoyo](#):
>
> And is this function valid?
> 
> ```arduino
> void func(int size)
> {int array[size];}
> 
> ```

Not always. This behaviour is compiler specific. See [Basic C++ Array question](https://forum.arduino.cc/t/basic-c-array-question/686236)

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 2, 2025, 9:24am UTC](https://forum.arduino.cc/t/initializing-array/1393001/9 "2025-07-02T09:24:42Z")

</div>

As other have said, our compiler supports VLA (variable length arrays) and so the code will work.

The array is on the stack and its scope is local to the function so when the function terminates the array is discarded. I’m assuming you intend to use it only with some other code that will be in the function too.

Note that when programming on small microcontrollers, it’s best to allocate your arrays to the maximum size you intend to support right from the start. This way, you avoid memory issues during execution, since memory on these devices is limited and unpredictable allocations at runtime can quickly cause problems. If your code might need that much memory at any point, you need to make sure it will be there so it’s best to have that reserved and available from the beginning. This ensures your program stays stable and avoids hard-to-track bugs like overflows or memory fragmentation.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 29, 2025, 9:24am UTC](https://forum.arduino.cc/t/initializing-array/1393001/10 "2025-12-29T09:24:51Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
