Local array sizeof()

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

a7

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.

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

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.

strcpy()

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

(as pointed out, not global arrays) and

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.

 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

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)

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

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

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!

:slight_smile: