An array is a named collection of elements of the same type. It is a nonempty sequence of data and occupies a group of contiguous storage locations. An array has a name, a set of elements, and a type.
An array name is a symbolic name for the whole sequence of data.
An array element is one member of the sequence of data. Each storage location holds one element of the array.
An array element name is an array name qualified by a subscript. See "Array Subscripts " for details.
You can declare an array in any of the following statements:
DIMENSION statement
COMMON statement
Type statements: BYTE, CHARACTER, INTEGER, REAL, and so forth
An array declarator specifies the name and properties of an array.
The syntax of an array declarator is:
a ( d [, d ] ... )
where:
a is the name of the array
d is a dimension declarator
A dimension declarator has the form:
[ dl:] du
where:
dl is the lower dimension bound
du is the upper dimension bound
An array must appear only once in an array declarator within a program unit (main program, subroutine, function, or block common). The compiler flags multiple or duplicate array declarations within the same unit as errors.
The number of dimensions in an array is the number of dimension declarators. The minimum number of dimensions is one; the maximum is seven. For an assumed-size array, the last dimension can be an asterisk.
The lower bound indicates the first element of the dimension, and the upper bound indicates the last element of the dimension. In a one-dimensional array, these are the first and last elements of the array.
Example: Array declarator, lower and upper bounds:
REAL V(-5:5)
In the above example, V is an array of real numbers, with 1 dimension and 11 elements. The first element is V(-5); the last element is V(5).
Example: Default lower bound of 1:
REAL V(1000)
In the above example, V is an array of real numbers, with 1 dimension and 1000 elements. The first element is V(1); the last element is V(1000).
Example: Arrays can have as many as 7 dimensions:
REAL TAO(2,2,3,4,5,6,10)
Example: Lower bounds other than one:
REAL A(3:5, 7, 3:5), B(0:2)
CHARACTER M(3,4)*7, V(9)*4
The array M has 12 elements, each of which consists of 7 characters.
The array V has 9 elements, each of which consists of 4 characters.
The following restrictions on bounds apply:
Both the upper and the lower bounds can be negative, zero, or positive.
The upper bound must be greater than or equal to the lower bound.
If only one bound is specified, it is the upper, and the lower is one.
In assumed-size arrays, the upper bound of the last dimension is an asterisk.
Each bound is an integer expression, and each operand of the expression is a constant, a dummy argument, or a variable in a common block. No array references or user-defined functions are allowed.
An adjustable array is an array that is a dummy argument or local array@ with one or more of its dimensions or bounds as an expression of integer variables that are either themselves dummy arguments, or are in a common block.
You can declare adjustable arrays in the usual DIMENSION or type statements. In f77, you can also declare adjustable arrays in a RECORD statement, if that RECORD statement is not inside a structure declaration block.
SUBROUTINE POPUP ( A, B, N ) COMMON / DEFS / M, L REAL A(3:5, L, M:N), B(N+1:2*N) ! These arrays are dummy args REAL C(N+1,2*N) ! This array is local
The restrictions are:
The size of an adjustable array cannot exceed the size of the corresponding actual argument.
In the first caller of the call sequence, the corresponding array must be dimensioned with constants.
You cannot declare an adjustable array in COMMON.
If the array is local to the routine, memory is allocated on entry to the routine and deallocated on return to the caller.@
An assumed-size array is an array that is a dummy argument, and which has an asterisk as the upper bound of the last dimension.
You can declare assumed-size arrays in the usual DIMENSION, COMMON, or type statements.
Ihe following f77 extensions allow you to:@
declare assumed-size arrays in a RECORD statement, if that RECORD statement is not inside a structure declaration block.
use an assumed-size array as a unit identifier for an internal file in an I/O statement.
use an assumed-size array as a runtime format specifier in an I/O statement.
Example: Assumed-size with the upper bound of the last dimension an asterisk:
SUBROUTINE PULLDOWN ( A, B, C ) INTEGER A(5, *), B(*), C(0:1, 2:*)
An assumed-size array cannot be used in an I/O list.
An array name with no subscripts indicates the entire array. It can appear in any of the following statements:
COMMON
DATA
I/O statements
NAMELIST
RECORD statements
SAVE
Type statements
In an EQUIVALENCE statement, the array name without subscripts indicates the first element of the array.
An array element name is an array name qualified by a subscript.
A subscript is a parenthesized list of subscript expressions. There must be one subscript expression for each dimension of the array.
The form of a subscript is:
( s [, s ] )
where s is a subscript expression. The parentheses are part of the subscript.
Example: Declare a two-by-three array with the declarator:
REAL M(2,3)
With the above declaration, you can assign a value to a particular element, as follows:
M(1,2) = 0.0
The above code assigns 0.0 to the element in row 1, column 2, of array M.
Subscript expressions have the following properties and restrictions:
A subscript expression is an integer, real, complex, logical, or byte expression. According to the FORTRAN Standard, it must be an integer expression.
A subscript expression can contain array element references and function references.
Evaluation of a function reference must not alter the value of any other subscript expression within the same subscript.
Each subscript expression is an index into the appropriate dimension of the array.
Each subscript expression must be within the bounds for the appropriate dimension of the array.
A subscript of the form ( L1, , Ln ), where each Li is the lower bound of the respective dimension, references the first element of the array.
A subscript of the form (U1, , Un), where each Ui is the upper bound of the respective dimension, references the last element of the array.
In the above example, the fourth element of V is set to zero.
Subscript expressions cannot exceed the range of INTEGER*4 in 32-bit environments. It is not controlled, but if the subscript expression is not in the range (-2147483648, 2147483647), then the results are unpredictable. When compiled for 64-bit environments, INTEGER*8 subscript expressions are allowed.
Array elements are usually considered as being arranged with the first subscript as the row number and the second subscript as the column number. This corresponds to traditional mathematical nxm matrix notation:
a1,1 |
a1,2 |
a1,3 |
... |
a1,m |
a2,1 |
a2,2 |
... |
|
a2,m |
... |
... |
ai,j |
... |
ai,m |
an,1 |
an,2 |
... |
|
an,m |
Element ai,j is located in row i, column j.
INTEGER*4 A(3,2)
The elements of A are conceptually arranged in 3 rows and 2 columns:
A(1,1) | A(1,2) |
A(2,1) | A(2,2) |
A(3,1) | A(3,2) |
Array elements are stored in column-major order.
Example: For the array A, they are located in memory as follows:
A(1,1) | A(2,1) | A(3,1) | A(1,2) | A(2,2) | A(3,2) |
The inner (leftmost) subscript changes more rapidly.