Setting size of Array at global variable setup?

Hi guys,

Would like to have an array that’s size is set at the initial setup of the OS, so running before void setup(), but am hitting an error, in that the size of the array needs to be a constant.

Thing is, I am happy for it to be a constant, but for its size to be set at setup based on the timestep I am using… So, ideally something like:

int timeStep = 60000;                        // timestep in ms
const int readsPerHour = 3600000 / timeStep; // e.g. 60s (60000ms) = readperHour = 60
int hourlyReadings[readsPerHour];            //  e.g. above --> hourlyReadings[60];

Thinking this is just a n00b question and I flat out cant do it… But, if there is a way, possibly, it would help.

You’re missing a const in the definition of timeStep.

const int timeStep = 60000;     

Basically, everything that’s used to calculate readsPerHour must be const (cannot be changed at runtime) because otherwise the sizing of the array is no longer constant and cannot be statically allocated.

3 Likes

Oh wow, that makes complete sense. Thanks!

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