Developer Guide and Reference

ID 767251
Date 10/31/2024
Public
Document Table of Contents

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:

  • Data type

    An array can have any intrinsic or derived type. The data type of an array (like any other variable) is specified in a type declaration statement or implied by the first letter of its name. All elements of the array have the same type and kind parameters. If a value assigned to an individual array element is not the same as the type of the array, it is converted to the array's type.

  • Rank

    The rank of an array is the number of dimensions in the array. An array can have up to 31 dimensions. A rank-one array represents a column of data (a vector), a rank-two array represents a table of data arranged in columns and rows (a matrix), a rank-three array represents a table of data on multiple pages (or planes), and so forth.

  • Bounds

    Arrays have a lower and upper bound in each dimension. These bounds determine the range of values that can be used as subscripts for the dimension. The value of either bound can be positive, negative, or zero.

    The bounds of a dimension are defined in an array specification.

  • Size

    The size of an array is the total number of elements in the array (the product of the array's extents).

    The extent is the total number of elements in a particular dimension. The extent is determined as follows: upper bound - lower bound + 1. If the value of any of an array's extents is zero, the array has a size of zero.

  • Shape

    The shape of an array is determined by its rank and extents, and can be represented as a rank-one array (vector) where each element is the extent of the corresponding dimension.

    Two arrays with the same shape are said to be conformable. A scalar is conformable to an array of any shape.

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.

This section also discusses:

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)