I'd like to settle this once and for ever, because I've been fumbling around this and failing, and then went for some workarounds.
My programming background is Java, C#, Python and handful of older programming languages (ASM, for god's sake), but relatively little of C++. The Arduino where I code now, is Mega.
So, I am basically looking for the following:
- Passing array as function parameter
Here's the function
void processArray(int a[], int size)
{
for (int i=0; i<size; i++)
{
// Do processing with a[i]
}
}
This works fine:
void setup()
{
int a[4] = {0, 1, 2, 3};
processArray(a, 4);
}
But why can't I do the following?
void setup()
{
processArray({0, 1, 2, 3}, 4);
}
Am I stuck to waste a variable and line of code always I want to pass an array to a function?
-
If I want to store an array in class variable, how do I do it? I am interested in both fixed size and variable size array case. How to declare the class variable in the header file and how to code the method that takes the array parameter and stores the data in the class variable?
-
If I want to establish dynamic array (ie, variable size, if there's enough space in memory), how do I do it?
-
If I want to establish variable size array of objects, how do I do it? With "object" I here understand instances of my custom class.