Initializing an Array - Arduino Documentation

I have a question on the document located at https://www.arduino.cc/en/Reference/Array where they state an example of initializing an array as follows:

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

Later on they explain that "Finally you can both initialize and size your array, as in mySensVals. Note that when declaring an array of type char, one more element than your initialization is required, to hold the required null character."
That phrase would apply to another example, in which they do this:

char message[6] = "hello";

That does make sense, because it's char type, and "hello" word has 6 characters.

However, coming back to the first example, that's not a character type array. It's Integer type. That seems confusing :confused:

Because later on they state another example:

int myArray[10]={9,3,2,4,3,2,7,8,9,11};

There we go, they initialized the array with ten spaces, the array has 10, not 9, 10 elements, as it should have. However that doesn't match the first example I'm referring to, in which he initializes the array with 6 spaces but loads only 5 elements into it.

I'm not trying to be smart here, just curious because I'm actually not totally understanding that example. If you find it wrong, please correct it because it's leading to confusion. Else, please add some further explanation on the document itself to clarify Java developers newbies on C, like myself.

Thank you for your attention, you've built a great website with plenty handful documentation. :slight_smile:

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

Declaring the array with 6 elements but only values does no harm apart from wasting a couple of bytes of memory.

As it says before the examples on the page that you referenced

All of the methods below are valid ways to create (declare) an array.

That does not mean that they are the most effective or efficient way to declare an array.

Welcome to arduino (and it's quirks). It' will compile and run as intended. The extra element acts like a null terminating, helps prevent unwanted reading of adjacent data within the program space.

There are a few other caveats to note:
Divide is only fast for powers of 2n
digitalRead() & digitalWrite() have 40 lines or so each so not so good for fast, tight loops. digitalRead() WILL disable PWM on a PWM pin IF you call digitalRead() on the same pin.

Happy coding.

I understand your confusion but note that there are 2 sentences:

  • sentence 1: "Finally you can both initialize and size your array, as in mySensVals."
  • sentence 2: "Note that when declaring an array of type char, one more element than your initialization is required, to hold the required null character."

Both sentences are true, but only the first refer to mySensVals.

There we go, they initialized the array with ten spaces, the array has 10, not 9, 10 elements, as it should have. However that doesn't match the first example I'm referring to, in which he initializes the array with 6 spaces but loads only 5 elements into it.

The website may need improving, however, here are some explanations for you to clear some things up.

char message[6] = "hello";

The right hand side of the '=' is a string literal. It implicitly includes a null character at the end. So there are six elements, just five non-null characters, Just like you already mentioned.

Now, to make things clearer, here are a few different initializations.

int mySensVals[6];

This will not initialize the data, it will hold whatever is in memory already. However if this is a global, then it will be set to zero. Not because of the array declaration, but how the language will default global/static storage with zero before initialization. A local array (in a function) will contain whatever was on the stack before its creation.

int mySensVals[6] = {};

This is a default initialization. As the type is an int, the default is zero, so all elements are set to zero by initialization.

int mySensVals[6] = {1,2,3,4,5,6};

This declaration will initialize every value to a certain value, nothing special here.

int mySensVals[6] = {1,2,3};

In this declaration, there are three values set. However what has not have been mentioned is: all elements following the explicit initialization, are default initialized.

int mySensVals[] = {1,2,3,4};

This declaration relies upon an initialization. The number of values provided determines the array length.

Hope this helps.