user45779: I am working with Arduino UNO connected with BV4612 display.
I am working on a simple code that controls several water pumps. Each pump has several modes it can run in. I store these modes in an array.
The number of water pumps may vary, and I'd like to store that value in EEPROM (either arduino's or display's).
When the pump count is fixed, I use fixed value, defined by macro:
#define     PUMP_COUNT      18      //How many pumps we have

uint8_t pumpModes[PUMP_COUNT];

void setup(){...}
void loop(){...}

I'd like to get rid of fixed defined value of PUMP_COUNT, and rather load it from EEPROM, or save the new pump count into the EEPROM if needed.
However, I can't declare an array if I can't read from EEPROM before setup() function like this (from address 32 for example):
uint8_t pumpModes[EEread(32)];

void setup(){...}
void loop(){...}

Is there a solution to get the value from EEPROM so I can declare global array's length with it?
Thanks.

user4143: You can't create variable length arrays in C. That's not how C works.
Instead you have to create an array that is big enough to store the maximum number of pumps you want to be able to deal with. 
The array "size" is then just a number in a variable obtained from wherever you like (including EEPROM).
It doesn't matter if you don't use all of the array. The rest just sits there twiddling its metaphorical thumbs.
The other option, if you are only going to define the array once at startup, or very rarely change the size, is to use malloc():
uint8_t *pumps;
uint8_t nPumps = 0;

void setup() {
    nPumps = EEPROM.read(32);
    pumps = (uint8_t *)malloc(nPumps);
}

You can change the array size then with:
nPumps = 8; // for example
pumps = realloc(pumps, nPumps);

And dispose of the array with:
nPumps = 0;
free(pumps);

It's best to try not to change the array size once created, since that can make swiss cheese of your heap, and is not a friendly thing to do on a small microcontroller. If you can decide the array size once in setup and keep it the same all the time the program is running this is a safe operation. Otherwise it becomes risky.

45779: <p>I am working with Arduino UNO connected with BV4612 display.
I am working on a simple code that controls several water pumps. Each pump has several modes it can run in. I store these modes in an array.
The number of water pumps may vary, and I'd like to store that value in EEPROM (either arduino's or display's).</p>

<p>When the pump count is fixed, I use fixed value, defined by macro:</p>

<pre><code>#define     PUMP_COUNT      18      //How many pumps we have

uint8_t pumpModes[PUMP_COUNT];

void setup(){...}
void loop(){...}
</code></pre>

<p>I'd like to get rid of fixed defined value of PUMP_COUNT, and rather load it from EEPROM, or save the new pump count into the EEPROM if needed.
However, I can't declare an array if I can't read from EEPROM before setup() function like this (from address 32 for example):</p>

<pre><code>uint8_t pumpModes[EEread(32)];

void setup(){...}
void loop(){...}
</code></pre>

<p>Is there a solution to get the value from EEPROM so I can declare global array's length with it?</p>

<p>Thanks.</p>
 4143: <p>You can't create variable length arrays in C. That's not how C works.</p>

<p>Instead you have to create an array that is big enough to store the <em>maximum</em> number of pumps you want to be able to deal with. </p>

<p>The array "size" is then just a number in a variable obtained from wherever you like (including EEPROM).</p>

<p>It doesn't matter if you don't use all of the array. The rest just sits there twiddling its metaphorical thumbs.</p>

<p>The other option, if you are only going to define the array once at startup, or very rarely change the size, is to use <code>malloc()</code>:</p>

<pre><code>uint8_t *pumps;
uint8_t nPumps = 0;

void setup() {
    nPumps = EEPROM.read(32);
    pumps = (uint8_t *)malloc(nPumps);
}
</code></pre>

<p>You can change the array size then with:</p>

<pre><code>nPumps = 8; // for example
pumps = realloc(pumps, nPumps);
</code></pre>

<p>And dispose of the array with:</p>

<pre><code>nPumps = 0;
free(pumps);
</code></pre>

<p>It's best to try not to change the array size once created, since that can make swiss cheese of your heap, and is not a friendly thing to do on a small microcontroller. If you can decide the array size once in setup and keep it the same all the time the program is running this is a safe operation. Otherwise it becomes risky.</p>
