We use arrays for vectors. So
double c[5];
allocates space for five consecutive double precision values called
the elements of the array, or components of the vector. Subscripts
are indicated with brackets. By convention the first element has
subscript zero c[0], so the fifth element is c[4]. This standard is sometimes called zero-base
subscripting. It can lead to confusion if you aren't expecting it.
We can also initialize an array in the declaration statement. The
initializer is a comma-separated list, enclosed in braces:
double c[5] = {1.2, -3.7, 5.4, 99.5, -45};
To declare h to be a matrix with 3 rows and 2 columns we write
double h[3][2];
In Fig.
we show the resulting storage allocation.
There are more convenient and elegant ways to handle matrices, but this method works.
A matrix can also be initialized in the declaration. The order of
assignment follows the storage order shown in Fig.
.
The identifiers c[2] and h[1][2] refer to
double precision values. But it is a convention that the
unsubscripted identifiers c and h are
pointers to the first element. So we could assign the address of c to the pointer variable ap:
ap = c;
This assignment is equivalent to
ap = &c[0];
In the former case we should not use an ampersand, because c is already a pointer by convention. In the latter case c[0] is a number, so we need the ampersand to specify its
address.
Following this assignment, we could then change the value of the third
element of c to 7.5 this way:
ap[2] = 7.5;
Any pointer can be turned into an implied array this way.