Arrays
Rows and tables of storage.
Arrays are a convenient way of
grouping a lot of variables under a single variable name. Arrays are like
pigeon holes or chessboards, with each compartment or square acting as a
storage place; they can be one dimensional, two dimensional or more dimensional!
An array is defined using square brackets []. For example: an array of three
integers called "triplet" would be declared like this:
int triplet[3];
Notice that there is no space
between the square bracket [ and the name of the array. This statement would cause space
for three integers type variables to be created in memory next to each other as
in the diagram below.
------------------------------------
int
triplet: | | | |
------------------------------------
The number in the square brackets of
the declaration is referred to as the `index' (plural: indicies) or `subscript'
of the array and it must be an integer number between 0 and (in this case) 2.
The three integers are called elements of the array and they are referred to in
a program by writing:
triplet[0]
triplet[1]
triplet[2]
Note that
the indicies start at zero and run up to one less than the number which is
placed in the declaration (which is called the dimension of the array.)
0 comments:
Post a Comment