Pages: [1]   Go Down
Author Topic: Error in int array declaration of array reference page?  (Read 849 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 1
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
just had a look at http://arduino.cc/en/Reference/Array
and came across what I think is an error...

In section "Creating (Declaring) an Array" it states:
int mySensVals[6] = {2, 4, -8, 3, 2};

I count 5 items and as this is an int array and not a char "array" declaration, there is no last implicit last '\0' item which will add +1 to the item count.

Wrong?

Cheers,

Alex.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The number of explicit initializers (the values in the curly braces) does not have to match the size of the array, as long as there are not more of them.

When the size is not specified, the compiler will determine the size of the array.

I'm not sure that there is ever a situation where the number of initializers (other than 0) will reasonably not match the size of the array.

The page could do a better job of explaining this, but there is nothing technically wrong on that page.

Misleading? Perhaps. Incomplete, yes. Wrong, no.
Logged

Left Coast, USA
Offline Offline
Sr. Member
****
Karma: 5
Posts: 499
Sometimes I just can't help myself.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

...
I count 5 items and as this is an int array and not a char "array" declaration, there is no last implicit last '\0' item which will add +1 to the item count.

1. A declaration of a char array with a comma-separated list of chars in braces does not implicitly add an extra element for the zero byte.  Lots of char arrays are used for things other than C-style "strings."  On the other hand, if you initialize a char array with a string literal, and you don't declare the size of the array the array will be sized by the compiler to include the terminating zero byte.

2. For any kind of array with a declared size, if the number of elements in the initializer list is smaller than the declared size of the array, the remaining elements are initialized to zero.


Regards,

Dave
Logged

Pages: [1]   Go Up
Jump to: