Mikroelektronika MIKROE-350 Fiche De Données
ARRAYS
An array represents an indexed collection of elements of the same type (called the
base type). Since each element has a unique index, arrays, unlike sets, can mean-
ingfully contain the same value more than once.
base type). Since each element has a unique index, arrays, unlike sets, can mean-
ingfully contain the same value more than once.
Array Declaration
Array types are denoted by constructions in the following form:
type[array_length]
Each of elements of an array is numbered from
0
through
array_length
- 1. Every
element of an array is of
type
and can be accessed by specifying array name fol-
lowed by element’s index within brackets.
Here are a few examples of array declaration:
dim weekdays as byte[7]
dim samples as word[50]
dim samples as word[50]
main:
' Now we can access elements of array variables, for example:
samples[0] = 1
if samples[37] = 0 then
samples[0] = 1
if samples[37] = 0 then
' ...
Constant Arrays
Constant array is initialized by assigning it a comma-delimited sequence of values
within parentheses. For example:
within parentheses. For example:
' Declare a constant array which holds number of days in each month:
const MONTHS as byte[12] = (31,28,31,30,31,30,31,31,30,31,30,31)
Note that indexing is zero based; in the previous example, number of days in Jan-
uary is
uary is
MONTHS[0]
and number of days in December is
MONTHS[11]
.
The number of assigned values must not exceed the specified length. Vice versa is
possible, when the trailing “excess” elements will be assigned zeroes.
possible, when the trailing “excess” elements will be assigned zeroes.
For more information on arrays of
char
, refer to Strings.
138
MIKROELEKTRONIKA
- SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
Language Reference
mikroBasic PRO for AVR
CHAPTER 5