[SOLVED] Problem initializing an array with an integer from int()

I am trying to have an array size depend on a variable in the header section (before the loop).

I declare the array with the variable in the . The line before the array declaration I do some math, which is to give the size of the array. I know that the math gives the right answer as I print the variable to the serial monitor and it is the right answer (and appears to be an integer). I have put int( ) around the math to try to force it to be an integer, but it seems to not work, as if I use the variable in the array declaration it will not compile. Instead I get this error:

error: array bound is not an integer constant

Here is the code from this section (in the header, before loop):


int maxMovementTime = 5000;
int maxStillnessThreshold = 20;
int interval = 250;

int arrayLength = int((int(maxMovementTime / interval) * (100 + maxStillnessThreshold)) / 100);

boolean movementArray[arrayLength];


The math should give 24 based on these numbers, and it does when I print it to the serial monitor. If I put [24] instead of [arrayLength], then the program compiles fine.

Why is arrayLength not appearing to be an integer?

The key word in the error isn't integer. You've definitely got an integer. The key word is constant, which your int is not.

An array can only be sized using a constant value.

const byte arraySize = 12;
int myArray[arraySize];

I guess the 12 could be a value calculated from other variables, but I have never tried that.

...R

Robin2:
An array can only be sized using a constant value.

const byte arraySize = 12;
int myArray[arraySize];

I guess the 12 could be a value calculated from other variables, but I have never tried that.

...R

Inside a function GCC supports variable length arrays:
void loop(){
** bool movementArray[random(1,40)];**
}
//or through an input parameter
void func( int count ){
** bool movementArray[ count ];**
}

However when its global, the constant it refers too is a 'compile time constant'. You can use other variables, however they must also resolve to compile time constants.

const int r = random( 5, 50 );
int arr[r];
void setup(){}
void loop(){}

This produces: array bound is not an integer constant before ']' token

You need to understand the difference between compiliation and run time.

When your sketch is being compiled, the compiler needs to know how much space to reserve for your array. The compiler isn't going to run any code to calculate that value. So it needs to be spoon fed an exact value.

This works by virtue of sourceArraySize being declared as const

byte sourceArray[] = {1, 1, 0 , 1 , 0, 1, 0, 0};
const byte sourceArraySize = sizeof(sourceArray) / sizeof(sourceArray[0]);
byte targetArray [sourceArraySize];

Thanks all for the help! It was the “const” that was the problem.

I simply changed:

int arrayLength

to:

const int arrayLength

And that worked! Much appreciated!

You could make your sketch slightly more efficient by using

#define arrayLength yourValueHere

But the saving is minimal.