I want to create an array but specify it's size in the setup function (or some other function inside the sketch). I'm getting two kinds of error messages. If I set an int to hold the size of the array, the error message is
array bound is not an integer constant
And if I set a const int to hold the size of the array I'm getting this
assignment of read-only variable 'arraySize'
'arraySize' is the variable obviously.
This all makes sense to me, but I don't know if there is a way to manipulate the size of an array inside a function, let alone know how to implement this.
This all makes sense to me, but I don't know if there is a way to manipulate the size of an array inside a function, let alone know how to implement this.
What do you mean when you say "manipulate the size of an array" ?
Are you trying to change the size of an array that already exists or declare a new one, possibly with the same name ? How are the array that you are trying to manipulate and arraySize declared ? Note that if you declare an array in a function then its scope will be limited to that function.
Please post an example of your code that causes the error ?
Please post an example of your code that causes the error ?
Should have done that in the first post, sorry. An example
byte myArray[5]; // initialize an array the size of which might change during some process
int newSize;
const int numOfModules = 5;
byte numOfModuleElements[numOfModules] = {3, 3, 4, 2, 6}; // the sum of these values should change the size of the array
void setup()
{
for(int i = 0; i < numOfModules; i++){
newSize += numOfModuleElements[i];
}
// the result of the variable newSize is the size I'd like to assign to the array myArray
// maybe this changes also during some procedure so this loop should go in the main loop function
// if I declare the array again, it will cause an error, if I declare a new array, I won't have access to it in other functions
}
The thing is that I want to have access to that array in a few functions, that's why I'm declaring it globally.
There's "malloc()" but be very careful.
Is there some documentation on how to use this utility? If yes, can someone provide a link?
Firstly. As you have to type the number of modules into the array you may as well total them up and declare the array as normal.
Secondly. Move the calculation of the array size to the loop() function and declare the array there. Pass the array, or a pointer to it and its size, to any functions that use it.
Firstly. As you have to type the number of modules into the array you may as well total them up and declare the array as normal.
This is what I'm doing for now, but it could be helpful if I'm able to change the size of the array dynamically.
Secondly. Move the calculation of the array size to the loop() function and declare the array there. Pass the array, or a pointer to it and its size, to any functions that use it.
Just declare the array to be the maximum expected size and use a separate int variable to indicate how many array elements are actually in use.
I guess this is the easiest way, right? Won't this reserve memory that eventually might not be used? Is it very difficult to have flexibility in the memory you need over time? It looks like this goes more low level and I'm mainly comfortable with the Arduino language that's covered in its website's reference (and of course, not with all the stuff there).
Your example indicates an array of 23 bytes, not worth fussing over. If you start to run short of RAM later on you can try managing the array memory yourself with malloc() and realloc().
Yes, it's an example. In later use it's gonna grow, probably not dangerously. I'd still like to find some reference to read concerning use of malloc() and realloc() in Arduino.
Secondly. Move the calculation of the array size to the loop() function and declare the array there. Pass the array, or a pointer to it and its size, to any functions that use it.
How do you do that?
How do you do what ? Pass variables to functions or something else ?
Secondly. Move the calculation of the array size to the loop() function and declare the array there. Pass the array, or a pointer to it and its size, to any functions that use it.
How do you do that?
How do you do what ? Pass variables to functions or something else ?
How do you pass an array or a pointer to a function (if the array is not global...)?