|Fortran Language Reference Manual, Volume 1 - S-3692-51|
|Prev Section||Chapter 5. Declarations||Next Section|
An array object has the DIMENSION attribute. An array specification determines the array's rank, or number of dimensions. The extents of the dimensions may be declared or left unspecified. If they are left unspecified, the array must also have the ALLOCATABLE or POINTER attribute, or it must be a dummy argument.
The maximum rank of an array is 7. A scalar is considered to have rank 0.
An array with a deferred_shape_spec_list must have the POINTER or ALLOCATABLE attribute.
An array with an assumed_shape_spec_list or an assumed_size_spec must be a dummy argument.
For more information on specification_expr, see Section 22.214.171.124.
The number of sets of bounds specified is the number of dimensions (rank) of the array.
If the lower bound is omitted, the default value is 1.
The value of a lower bound or an upper bound can be positive, negative, or 0.
The subscript range of the array in a dimension is the set of integer values between and including the lower and upper bounds, provided the upper bound is not less than the lower bound. If the upper bound is less than the lower bound, the range is empty, the extent in that dimension is 0, and the size of the array is 0.
The expression for a bound could involve variables that cause the expression to have different values each time the procedure in which it is declared is executed. If so, the array must be a dummy argument, a function result, or an automatic array, in which case the actual bounds are determined when the procedure is entered. The bounds of such an array are unaffected by any redefinition or undefinition of the specification variables during the execution of the procedure.
The following are examples of entity-oriented explicit-shape array declarations:
REAL Q(-10:10, -10:10, 2) ! in a main program
SUBROUTINE EX1(Z, I, J) ! in a REAL, DIMENSION(2:I + 1, J) :: Z ! subroutine
The following are examples of attribute-oriented explicit-shape array declarations:
REAL Q DIMENSION Q(-10:10, -10:10, 2) ! in a main program
SUBROUTINE EX1(Z, I, J) ! in REAL Z ! a DIMENSION Z(2:I + 1, J) ! subroutine
The lower_bound of the assumed-shape array is the specified lower bound, if present; otherwise it is 1.
The rank is equal to the number of colons in the assumed_shape_spec_list.
The upper bound is the extent of the corresponding dimension of the associated array plus the lower bound minus 1.
An assumed-shape array must not have the POINTER or ALLOCATABLE attribute.
The following example shows an entity-oriented, assumed-shape array declaration:
REAL, DIMENSION(2:, :) :: X
The following example shows an attribute-oriented, assume-shaped array declaration:
SUBROUTINE EX2(A, B, X) REAL A(:), B(0:), X DIMENSION X(2:, :) INTENT(IN) A, B . . .
As another example, assume that EX2 is called by the following statement:
CALL EX2(U, V, W(4:9, 2:6))
Dummy argument X is an array with bounds (2:7, 1:5). The lower bound of the first dimension is 2 because X is declared to have a lower bound of 2. The upper bound is 7 because the dummy argument takes its shape from the actual argument W.
A deferred-shape array is either an array pointer or an allocatable array. An array pointer is an array that has the POINTER attribute. Its extent in each dimension is determined when the pointer is allocated or when a pointer assignment statement for the pointer is executed. An allocatable array is an array that has the ALLOCATABLE attribute. Its bounds, and thus its shape, are determined when the array is allocated. In both cases a colon specifies the declared bound; that is, the format of a deferred_shape_spec is defined as follows:
The rank is equal to the number of colons in the deferred_shape_spec_list.
The bounds of an allocatable array are specified in an ALLOCATE statement when the array is allocated.
The lower bound of each dimension of an array pointer is the result of the LBOUND(3i) intrinsic function applied to the corresponding dimension of the target. The upper bound of each dimension is the result of the UBOUND(3i) intrinsic function applied to the corresponding dimension of the target. This means that if the bounds are determined by allocation of the pointer, you can specify them. If the bounds are determined by pointer assignment, there are two possible interpretations:
If the pointer target is a named whole array, the bounds are those declared in the array declaration or those specified when the array was allocated.
If the pointer target is an array section, the lower bound is 1 and the upper bound is the extent in that dimension.
The bounds and shape of an array pointer or allocatable array are unaffected by any subsequent redefinition or undefinition of variables involved in determination of the bounds.
REAL, POINTER :: D(:, :), P(:) ! array pointers REAL, ALLOCATABLE :: E(:) ! allocatable array
The following examples show attribute-oriented, deferred-shaped array declarations:
REAL D DIMENSION D(:, :), P(:), E(:) POINTER D, P ALLOCATABLE E
An assumed-size array is a dummy argument array whose size is assumed from that of the associated actual argument. Only the size is assumed. The rank, extents, and bounds (except for the upper bound and extent in the last dimension) are determined by the declaration of the dummy array. The rules for argument association between an actual argument and an assumed-size array are as follows:
They must have the same initial array element.
Successive array elements are storage associated. For information on storage association, see Section 5.14.
Declarations for the dummy argument determine the rank. They also determine lower bounds for all dimensions and the extents and upper bounds for all dimensions except the last.
The size of the actual argument determines the size of the dummy argument as explained in this section.
The format of an assumed_size_spec is defined as follows:
The rank of an assumed-size array is the number of explicit-shape specifications plus one.
The size of an assumed-size array is determined as follows:
If the actual argument associated with the assumed-size dummy argument is an array of any type other than character, the size is that of the actual array.
If the actual argument associated with the assumed-size dummy array is an array element of any type other than character with a subscript order value of v in an array of size x, the size of the dummy argument is x - v +1. For information on array element order, see Section 6.4.7.
If the actual argument is a character array, character array element, or a character array element substring, and if it begins at character storage unit t of an array with c character storage units, the size of the dummy array is MAX(INT(( c - t +1)/ e ), 0) where e is the length of an element in the dummy character array.
If r is the rank of the array, the bounds of the first r-1 dimensions are those specified by the explicit-shape specification list, if present. The lower bound of the last dimension is the specified lower bound, if present; otherwise it is 1.
The expression for a bound may involve variables that cause the expression to have different values each time the procedure in which it is declared is executed. If so, the bounds are unaffected by any subsequent redefinition or undefinition of such variables involved in the determination of the bounds.
A function result must not be an assumed-size array.
An assumed-size array must not appear in a context where the shape of the array is required, such as a whole array reference.
The following examples show entity-oriented, assumed-size array declarations:
SUBROUTINE EX3(N, S, Y) REAL, DIMENSION(N, *) :: S REAL Y(10, 5, *) . . .
The following examples show attribute-oriented, assumed size array declarations:
SUBROUTINE EX3(N, S, Y) REAL S, Y DIMENSION S(N, *), Y(10, 5, *) . . .
The dimensions of an array can be specified by the appearance of a DIMENSION attribute or by the appearance of an array specification following the name of the array in a type declaration statement. Both a DIMENSION attribute and an array specification following the name can appear in a declaration statement. In this case, the array specification following the name overrides the array specification following the DIMENSION attribute. A format for a type declaration statement with a DIMENSION attribute is as follows:
type, DIMENSION ( array_spec) [, attribute_list ] :: entity_decl_list
Subject to the rules governing combinations of these attributes, attribute_list can contain the following:
The type declaration can also contain an initialization_expr. An array specification can also appear following a name in several different kinds of statements to declare an array. They are DIMENSION, ALLOCATABLE, POINTER, TARGET, and COMMON statements.
The DIMENSION statement is the statement form of the DIMENSION attribute and is defined as follows:
The DIMENSION statement also confers the DIMENSION attribute. It is subject to the same rules and restrictions as the DIMENSION attribute.
The following examples show entity-oriented declarations:
INTEGER, DIMENSION(10), TARGET, SAVE :: INDICES INTEGER, ALLOCATABLE, TARGET :: LG(:, :, :)
The following examples show attribute-oriented declarations:
INTEGER INDICES, LG(:, :, :) DIMENSION INDICES(10) TARGET INDICES, LG ALLOCATABLE LG SAVE INDICES
The following examples show the array specification in other statements:
INTEGER INDICES, LG TARGET INDICES(10), LG ALLOCATABLE LG(:, :, :) SAVE INDICES
The following example shows the array specification in a COMMON statement:
COMMON /UNIVERSAL/ TIME(80), SPACE(20, 20, 20, 20)