 # Understanding Arduino arrays

I’m reading the arduino reference for arrays:

http://arduino.cc/en/Reference/Array

And there are a few thing I dont understand very well.

At the begining of the reference it states this when creating an array:

``````int mySensVals = {2, 4, -8, 3, 2};
``````

but when accessing an array its this:

``````int myArray={9,3,2,4,3,2,7,8,9,11};
// myArray    contains 11
// myArray   is invalid and contains random information (other memory address)
``````

Notice that in the first one it creates an array of 6 with 5 different values, but int the second one creates an array of 10 with 10 different values.

So here goes my question. If I create an array of 6 elements like the first one but I give only 5 values. Which elements am I filling? The first element mysensvals will have a value of 0 or 2?

If I try to read mysensvals[-1] I’m reading a random memory value isn’t it?

Espada: So here goes my question. If I create an array of 6 elements like the first one but I give only 5 values. Which elements am I filling? The first element mysensvals will have a value of 0 or 2?

If you assign less inital values than the array has:

``````int mySensVals = {2, 4, -8, 3, 2};
``````

you always assign the first values. So the last value "mySensVals" will be initialized to 0.

Yes, if you are reading outside valid index values of the array, you get random values.

And if you are writing outside valid index values, you may change other values in RAM or even crash your program.

jurs: If you assign less inital values than the array has:

``````int mySensVals = {2, 4, -8, 3, 2};
``````

you always assign the first values. So the last value "mySensVals" will be initialized to 0.

Yes, if you are reading outside valid index values of the array, you get random values.

And if you are writing outside valid index values, you may change other values in RAM or even crash your program.

Thanks

If you only want the array sized to the values you are interested in, use:

``````int mySensVals[] = {2, 4, -8, 3, 2};
``````

Note that the definition no longer has the element count (i.e., the 6) present in the definition of mySensVals[]. If you do that, the compiler counts the values in your initializer list (five values above), and supplies that value so it appears that you wrote the definition as:

``````int mySensVals = {2, 4, -8, 3, 2};
``````

Leaving the element count out when an initializer list is present is preferred as compiler are really good at counting.

econjack: If you only want the array sized to the values you are interested in, use:

``````int mySensVals[] = {2, 4, -8, 3, 2};
``````

Note that the definition no longer has the element count (i.e., the 6) present in the definition of mySensVals[]. If you do that, the compiler counts the values in your initializer list (five values above), and supplies that value so it appears that you wrote the definition as:

``````int mySensVals = {2, 4, -8, 3, 2};
``````

Leaving the element count out when an initializer list is present is preferred as compiler are really good at counting.

And, later if you need to know how many elements there are, use this code:

``````[b]elements = (sizeof (array) / sizeof (*array));[/b]
``````

This automatically accounts for the size of any array element.

For example, if you had this:

float values[] = { 1.1, 2.2, 3.3, 4.4, 5.5; }

the sizeof (values) would be 20 (because each float is 4 bytes). But, sizeof (*values) gives you the size of the first element in the array, which is 4. So, 20 / 4 = 5 which is a count of how many elements are in the array.

If you used int instead, the numbers would be 10 and 2 and 10 / 2 = 5 so again you get the right value.

Make sense?

@Krupski: right, and it’s often implemented as a macro:

``````#define ELEMENTCOUNT(x) (sizeof(x) / sizeof(x))
``````