# Local array sizeof()

**URL:** <https://forum.arduino.cc/t/local-array-sizeof/1433373>\
**Category:** Programming\
**Created:** [March 1, 2026, 8:00pm UTC](https://forum.arduino.cc/t/local-array-sizeof/1433373 "2026-03-01T20:00:40Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [March 2, 2026, 2:39pm UTC](https://forum.arduino.cc/t/local-array-sizeof/1433373/21 "2026-03-02T14:39:18Z")

</div>

> [@dilberto](#):
>
> This ‘terminator mark’ is a value that is not supposed to occur in the array.

The classic example being the '\0' at the end of the meaningful part of a C character array.

a7

---

<div class="post-metadata">

**Author:** ![gilhad](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gilhad/32/996113_2.png) [@gilhad](https://forum.arduino.cc/u/gilhad)\
**Post date:** [March 2, 2026, 3:03pm UTC](https://forum.arduino.cc/t/local-array-sizeof/1433373/22 "2026-03-02T15:03:52Z")

</div>

I prefere to use pair (start address, length), as it enables using part of larger arrays without need to destroy values around the working part.

And I much more often need to (compute/manipulate) part of larger array, then to copy few bytes outside and use them as different array.

Also the pattern “global arrays by pointer, local arrays by copy” does not work good for manipulating parts of local arrays, where the function is used to change some values in array declared inside other function.

---

<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:** [March 2, 2026, 3:50pm UTC](https://forum.arduino.cc/t/local-array-sizeof/1433373/23 "2026-03-02T15:50:19Z")

</div>

> [@dilberto](#):
>
> but about a function that must handle variable-sized arrays,

`memset()` and `memcpy()` are standard library functions operating on variable length arrays, as well as `strcpy()`

---

<div class="post-metadata">

**Author:** ![dilberto](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dilberto/32/1126465_2.png) [@dilberto](https://forum.arduino.cc/u/dilberto)\
**Post date:** [March 2, 2026, 4:11pm UTC](https://forum.arduino.cc/t/local-array-sizeof/1433373/24 "2026-03-02T16:11:19Z")

</div>

Yeah!

But for these functions, you have to pass the array size to the function, whereas my goal was to show the O.P. a way to pass the array to the function without having to pass its size.

---

<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:** [March 2, 2026, 4:44pm UTC](https://forum.arduino.cc/t/local-array-sizeof/1433373/25 "2026-03-02T16:44:55Z")

</div>

> [@dilberto](#):
>
> show the O.P. a way to pass the array to the function without having to pass its size.

`strcpy()`

---

<div class="post-metadata">

**Author:** ![kenb4](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@kenb4](https://forum.arduino.cc/u/kenb4)\
**Post date:** [March 2, 2026, 8:22pm UTC](https://forum.arduino.cc/t/local-array-sizeof/1433373/26 "2026-03-02T20:22:02Z")

</div>

Hopefully by now, we can agree It is rare to want to make entire copies of arbitrarily sized blocks of memory. Even in easier languages, passing an array passes a reference; no copy is made.

Given OP's two scenarios

> [@](#):
>
> ```arduino
> setup(){
> int global_array[] = {1,2,3};
> int anotherglobal_array[] = {1,2,3,4};
> int thirdglobal_array[] = {1,2,3,4,5};
> }
> 
> ```

(as pointed out, not global arrays) and

> [@](#):
>
> `myfunction (int{1,2,3,4,5},11]; `
> 
> where {1,2,3,4,5} turns into a local array of five ints .

the distinction might be between "named" arrays and ad-hoc or anonymous arrays.

For the latter, C as of C99 offers _compound literals_ that are pretty close, e.g.

```cpp
 some_fun((int[]){1,2,3,4,5})

```

which looks like a combination of a cast -- great, more parentheses -- and brace initializer. You can use them in `.c` files in your sketch, but such code is harder to integrate with all the other code, which is C++.

The C++ standard library is not included with AVR, but otherwise you can use `std::initializer_list`

```cpp
unsigned long sumOf(std::initializer_list<unsigned long> const &a) {
  Serial.print("got: ");
  Serial.println(a.size());
  unsigned long t = 0;
  for (auto i : a) { // range-based for loop
    t += i;
  }
  return t;
}

void setup() {
  Serial.begin(115200);
  Serial.println(sumOf({ 2, 4, 6, millis() }));
  Serial.println(sumOf({ 1, 2, 3, 5, 7 }));
}

void loop() {}

```

(There are ways of just adding all the elements together that use less code.) Again, no local copy is made; the temporary array (or however it is actually implemented) is owned by the caller, and will likely disappear by the time the calling statement is done (perhaps to be overwritten by the next stack-allocated thing)

---

<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:** [March 2, 2026, 10:41pm UTC](https://forum.arduino.cc/t/local-array-sizeof/1433373/27 "2026-03-02T22:41:15Z")

</div>

> [@peteyf](#):
>
> That's stupid. Not you, the C++ people.

Sometimes people don't even know enough to know how much they don't know.

---

<div class="post-metadata">

**Author:** ![build\_1971](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/build_1971/32/689930_2.png) [@build\_1971](https://forum.arduino.cc/u/build_1971)\
**Post date:** [March 3, 2026, 5:05am UTC](https://forum.arduino.cc/t/local-array-sizeof/1433373/28 "2026-03-03T05:05:07Z")

</div>

"Now we know everything about nothing"  
--\> Einstein after solving the Schrodinger equations for the hydrogen atom.

---

<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:** [March 3, 2026, 6:51am UTC](https://forum.arduino.cc/t/local-array-sizeof/1433373/29 "2026-03-03T06:51:11Z")

</div>

The définition of a subject matter expert : An expert who keeps zooming in on a field is someone who learns more and more about less and less, until eventually he knows absolutely everything about absolutely nothing!

🙂

---

<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:** [August 30, 2026, 6:51am UTC](https://forum.arduino.cc/t/local-array-sizeof/1433373/30 "2026-08-30T06:51:59Z")

</div>

Closed on August 30, 2026, 6:51am UTC

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

[Previous page](https://forum.arduino.cc/t/local-array-sizeof/1433373.md?page=1)
