# For & arrays - confirmation

Hi everyone,
I would like to know if I could simplify this part of my sketch :

by this :

for (byte i = 1; i < 10; i++)
{
}

Not quite.
Check the range of array indices generated by the for loop.
(Most people index C arrays from zero)
Please remember to use code tags when posting code

You could certainly do what you suggest but all that would happen at the end of the for loop is that the variable k would have the value read from EEPROM address 9

You need to read up on arrays and do something like

``````char k;  //declare an array of chars with 10 levels numbered 0 to 9
for (int byte i = 0; i < 10; i++)
{
}
``````

If what you are reading is to be used as a C style string then the array will need to have 11 levels and you will need to add the trailing zero to level 10 to make it into a string.

Thanks. So, as I need to read the 200 first values in the EEPROM, the part of my sketch will definitely look like this :

``````char k;  //declare an array of chars with 10 levels numbered 0 to 9

for (int byte i = 1; i < 201; i++)
{
}
``````

So here, if I understand well the sketch, the variable k124 for instance will take her value in the adress 124 of the EEPROM.

I am right ?

No, you are wrong.
There is no variable k124.

so, where is assigned the value of the adress 124 of the EEPROM ?

In k

Remember that C arrays index from zero.

Ah ok, thanks. And I will be able to use this variable like another ?
For instance to print her value on a LCD, like this ?

``````lcd.clear();
lcd.print(k);
``````

``````char k;  //declare an array of chars with 200 levels numbered 0 to 199

for (int byte i = 1; i < 201; i++)  //i will have a value of 200 during the final iteration of the for loop
{
k[i] = EEPROM.read(i);  //**THERE IS NO LEVEL 200 IN THE ARRAY**
}
``````

Be VERY careful with array index variables as it is all to easy to overwrite memory that is not in the array, as above.

As previously pointed out, array levels start at 0, not 1

Ok, so the code would look more like this ?

``````char k;  /*declare an array of chars with 200 levels numbered 0 to 199*/

for (int byte i = 0; i < 200; i++)  /* i will have a value of 199 during the final iteration of the for loop*/
{
k[i] = EEPROM.read(i);  //**THERE IS NO LEVEL 200 IN THE ARRAY**
}
``````

Yes, that's better, except they're "ELEMENTS" not "LEVELS", thus `//**THERE IS NO ELEMENT 200 IN THE ARRAY**`

That's better

Now look at EEPROM.put() and EEPROM.get() to see how to save or load the whole array with one line of code.

AWOL:
they're "ELEMENTS" not "LEVELS"

That's my fault because I referred to them as levels earlier in the thread

Ok, thanks a lot.

Can I put this “for” loop in the void setup, to avoid 200 lines of codes ?

So to replace this :

``````char l0 = EEPROM.read (0);
// [...]
``````

by this :

``````char k;  /*declare an array of chars with 200 levels numbered 0 to 199*/

for (int byte i = 0; i < 200; i++)  /* i will have a value of 199 during the final iteration of the for loop*/
{
l[i] = EEPROM.read(i);  //**THERE IS NO ELEMENT 200 IN THE ARRAY**
}
``````

In fact, I don’t know if the void setup can support loops.

The setup function is just that - a function.
Anything you can do in the loop function, you can do in setup.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7731334,"math_prob":0.83471674,"size":420,"snap":"2021-31-2021-39","text_gpt3_token_len":136,"char_repetition_ratio":0.3966346,"word_repetition_ratio":0.0,"special_character_ratio":0.42380953,"punctuation_ratio":0.25233644,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95701313,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-25T22:22:34Z\",\"WARC-Record-ID\":\"<urn:uuid:82f9fcc5-b052-4b5f-bb92-91a376df0d1a>\",\"Content-Length\":\"46975\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:505d44a5-931d-401c-be6a-0f6601c68c91>\",\"WARC-Concurrent-To\":\"<urn:uuid:1d3d0fe8-9ceb-4a7d-beb7-09dbbbf9a46b>\",\"WARC-IP-Address\":\"184.104.202.141\",\"WARC-Target-URI\":\"https://forum.arduino.cc/t/for-arrays-confirmation/550324\",\"WARC-Payload-Digest\":\"sha1:MRJMJRXK3ZAPIQNZ62SK624O5DWS4Q3F\",\"WARC-Block-Digest\":\"sha1:EB5PC6TPWYM3DFLEBD4HA5BQXC34ON76\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780057775.50_warc_CC-MAIN-20210925202717-20210925232717-00007.warc.gz\"}"}