2.10. Structure of an array
Let me explain more about x[i], and what the square brackets mean.
x is a variable: it refers to refers to a chunk of memory that we
allocated for storage of a sequence of numbers. It is a type of array. Being
a sequence, it is a one-dimensional array. Arrays are not necessarily one-dimensional.
They can be two-dimensional arrays (e.g. for tables) or n-dimensional
arrays for more complex kinds of data. Arrays are basically n-dimensional
arrangements of a particular type of objects, in this case, integers,
short int’s. So x is just a one-dimensional sequence of
shorts.
Figure 2.8. Structure of a typical one-dimensional
array, x
References to elements of x
|
|
Contents of x
|
x[0]
|
|
32000
|
x[1]
|
|
30601
|
x[2]
|
|
26529
|
:
:
|
|
:
:
|
x[7999]
|
|
31606
|
The first element in it is referred to as x[0], and the i
’th element is called x[i]. The final element of an array a
of n elements is a[n-1], note, because the first element
is numbered a[0]. The cells are consecutive, and the address of the
first cell is very special, as it is also the address of the whole array.
The address of the first cell can be referred to by this notation with an
ampersand at the beginning, &x. We can use that address to refer
to an array as a whole. If we want to submit the array to a function or procedure,
we can refer to the array by its first element, and that is sufficient to
pass the array to another part of the program. By passing the address round,
you can tell other parts of the program where the array is, that is, where
it begins. Other than that, we are not normally interested in the address
— the actual location in memory — of an array or its cells. The following
may be a useful reference:
*x
|
A pointer to the first cell of x.
|
x[0]
|
Contents of the first cell of x.
|
x[i]
|
Contents of the i+1st cell.
|