3.4.2 Arrays

An array is a set of scalar elements that have the same type and kind parameters. Any object that is declared with an array specification is an array. Arrays can be declared by using a type declaration statement, or by using a DIMENSION, COMMON, ALLOCATABLE, POINTER, or TARGET statement.

An array can be referenced by element (using subscripts), by section (using a section subscript list), or as a whole. A subscript list (appended to the array name) indicates which array element or array section is being referenced.

A section subscript list consists of subscripts, subscript triplets, or vector subscripts. At least one subscript in the list must be a subscript triplet or vector subscript.

When an array name without any subscripts appears in an intrinsic operation (for example, addition), the operation applies to the whole array (all elements in the array).

An array has the following properties:

The name and rank of an array must be specified when the array is declared. The extent of each dimension can be constant, but does not need to be. The extents can vary during program execution if the array is a dummy argument array, an automatic array, an array pointer, or an allocatable array.

A whole array is referenced by the array name. Individual elements in a named array are referenced by a scalar subscript or list of scalar subscripts (if there is more than one dimension). A section of a named array is referenced by a section subscript.

Examples

The following are examples of valid array declarations:

  DIMENSION    A(10, 2, 3)            ! DIMENSION statement
  ALLOCATABLE  B(:, :)                ! ALLOCATABLE statement
  POINTER      C(:, :, :)             ! POINTER statement
  REAL, DIMENSION (2, 5) :: D         ! Type declaration with
                                      !    DIMENSION attribute

Consider the following array declaration:

INTEGER L(2:11,3)

The properties of array L are as follows:

Data type:  INTEGER 
Rank:  2 (two dimensions) 
Bounds:  First dimension: 2 to 11 
  Second dimension: 1 to 3 
Size:  30; the product of the extents: 10 x 3 
Shape:  (/10,3/) (or 10 by 3); a vector of the extents 10 and 3 

The following example shows other valid ways to declare this array:

DIMENSION L(2:11,3)
INTEGER, DIMENSION(2:11,3) :: L
COMMON L(2:11,3)

The following example shows references to array elements, array sections, and a whole array:

REAL B(10)           ! Declares a rank-one array with 10 elements

INTEGER C(5,8)       ! Declares a rank-two array with 5 elements in
                     !   dimension one and 8 elements in dimension two
...
B(3) = 5.0           ! Reference to an array element
B(2:5) = 1.0         ! Reference to an array section consisting of
                     !   elements: B(2), B(3), B(4), B(5)
...
C(4,8) = I           ! Reference to an array element
C(1:3,3:4) = J       ! Reference to an array section consisting of
                     !   elements:  C(1,3) C(1,4)
                     !              C(2,3) C(2,4)
                     !              C(3,3) C(3,4)
B = 99               ! Reference to a whole array consisting of
                     !   elements: B(1), B(2), B(3), B(4), B(5),
                     !   B(6), B(7), B(8), B(9), and B(10)

For More Information:


Previous Page Next Page Table of Contents