# Array Constructors

An array constructor can be used to create and assign values to rank-one arrays and array constants. An array constructor takes one of the following forms:

(/ac-spec/)

[ac-spec]

ac-spec | Is one of the following: type-spec :: or [type-spec ::] ac-value-list
type-spec | Is a type specifier.
ac-value-list | Is a list of one or more expressions or implied-DO loops.

An implied-DO loop in an array constructor takes the following form:

(ac-value-list, [integer-type-spec::] do-variable = expr1, expr2 [,expr3])

integer-type-spec | Is INTEGER [kind-selector].
kind-selector | Is ( [KIND=] n ).
n | Is a constant expression whose value is 1, 2, 4, or 8.
do-variable | Is the name of a scalar integer variable. Its scope is that of the implied-DO loop.
expr | Is a scalar integer expression. The expr1 and expr2 specify a range of values for the loop; expr3 specifies the stride. The expr3 must be a nonzero value; if it is omitted, it is assumed to be 1.

## Description

If type-spec is omitted, each ac-value-list expression must have the same type and kind type parameters, and the same length parameters. In this case, the array constructed has the same type as the ac-value-list expressions.

If type-spec appears, it specifies the type and type parameters of the array constructor. Each ac-value-list expression must be compatible with intrinsic assignment to a variable of this type and type parameters. Each value is converted to the type parameters of the array constructor in accordance with the rules of intrinsic assignment.

If type-spec specifies an intrinsic type, each ac-value-list expression must be of an intrinsic type that conforms with a variable of type type-spec.

If type-spec specifies a derived type, all ac-value-list expressions must be of that derived type and must have the same kind type parameter values as specified by type-spec.

A do-variable of an implied-DO that is in another implied-do must not appear as the do-variable of a containing implied-DO.

If the sequence of values specified by the array constructor is empty (an empty array expression or the implied-DO loop produces no values), the rank-one array has a size of zero.

An ac-value is interpreted as follows:

Form of ac-value | Result
--- | ---
A scalar expression | Its value is an element of the new array.
An array expression | The values of the elements in the expression (in array element order) are the corresponding sequence of elements in the new array.
An implied-DO loop | It is expanded to form a list of array elements under control of the DO variable (like a DO construct).

The following shows the three forms of an ac-value:

      C1 = (/4,8,7,6/)                   ! A scalar expression
      C2 = (/B(I, 1:5), B(I:J, 7:9)/)    ! An array expression
      C3 = (/(I, INTEGER :: I=1, 4)/)    ! An implied-DO loop

You can also mix these forms, for example:

      C4 = (/4, A(1:5), (I, I=1, 4), 7/)

If every expression in an array constructor is a constant expression, the array constructor is a constant expression.

If type-spec is omitted, Intel® Fortran allows the numeric expressions to be of different kind types, but not different types. The resultant numeric array is the type and kind type of the first expression in the ac-value-list. For example:

     INTEGER, PARAMETER :: K(2) = [4, 8]
    ! The following line prints 4.000000       8.00000000000000
    PRINT *, REAL (K(1), K(1)), REAL (K(2), K(2))
    ! The following line prints 4.000000       8.000000
    PRINT *, [ (REAL (K(I), K(I)), I=1,20]

If type-spec is omitted, Intel® Fortran allows the character expressions to be of different character lengths. The length of the resultant character array is the maximum of the lengths of the individual character expressions. For example:

    print *,len ( (/'a','ab','abc','d'/) )
    print *,'++'//(/'a','ab','abc','d'/)//'--'

This causes the following to be displayed:

               3
     ++a  --++ab --++abc--++d  --

If an implied-DO loop is contained within another implied-DO loop (nested), they cannot have the same DO variable (do-variable).

To define arrays of more than one dimension, use the RESHAPE intrinsic function.

The following are alternative forms for array constructors:

- Square brackets (instead of parentheses and slashes) to enclose array constructors; for example, the following two array constructors are equivalent:

        INTEGER C(4)
        C = (/4,8,7,6/)
        C = [4,8,7,6]

- A colon-separated triplet (instead of an implied-DO loop) to specify a range of values and a stride; for example, the following two array constructors are equivalent:

        INTEGER D(3)
        D = (/1:5:2/)              ! Triplet form - also [1:5:2]
        D = (/(I, I=1, 5, 2)/)     ! implied-DO loop form

  The stride is optional; it defaults to 1. For example, the following two array constructors are equivalent:

        INTEGER E(5)
        E = (/1:5/)              ! Triplet form with default stride – also [1:5]
        E = (/(I, I=1, 5)/)      ! implied-DO loop form

## Examples

The following example shows an array constructor using an implied-DO loop:

      INTEGER ARRAY_C(10)
      ARRAY_C = (/(I, I=30, 48, 2)/)

The values of ARRAY_C are the even numbers 30 through 48.

Implied-DO expressions and values can be mixed in the value list of an array constructor. For example:

     INTEGER A(10)
     A = (/1, 0, (I, I = -1, -6, -1), -7, -8 /)
     ! Mixed values and implied-DO in value list.

This example sets the elements of A to the values, in order, 1, 0, -1, -2, -3, -4, -5, -6, -7, -8.

The following example shows an array constructor of derived type that uses a structure constructor:

    TYPE EMPLOYEE
        INTEGER ID
        CHARACTER(LEN=30) NAME
    END TYPE EMPLOYEE
    TYPE(EMPLOYEE) CC_4T(4)
    CC_4T = (/EMPLOYEE(2732,"JONES"), EMPLOYEE(0217,"LEE"),     &
               EMPLOYEE(1889,"RYAN"), EMPLOYEE(4339,"EMERSON")/)

The following example shows how the RESHAPE intrinsic function can be used to create a multidimensional array:

      E = (/2.3, 4.7, 6.6/)
      D = RESHAPE(SOURCE = (/3.5, (/2.0, 1.0/), E/), SHAPE = (/2,3/))

D is a rank-two array with shape (2,3) containing the following elements:

       3.5    1.0    4.7
       2.0    2.3    6.6

The following shows another example:

     INTEGER B(2,3), C(8)
     ! Assign values to a (2,3) array.
     B = RESHAPE((/1, 2, 3, 4, 5, 6/),(/2,3/))
     ! Convert B to a vector before assigning values to
     ! vector C.
     C = (/ 0, RESHAPE(B,(/6/)), 7 /)

Consider the following derived-type definition:

    TYPE EMPLOYEE
     INTEGER AGE
     CHARACTER (LEN = 60) NAME
    END TYPE EMPLOYEE

The following equivalent lines use the above type to construct a derived-type array value:

    (/ EMPLOYEE (45, ’OLIVER’), EMPLOYEE (30, ’ONEIL’) /)
    [EMPLOYEE (45, ’OLIVER’), EMPLOYEE (30, ’ONEIL’)]

The following example shows an array constructor that specifies a length type parameter:

    (/ CHARACTER(LEN=8) :: 'Andrews', 'Donahue', 'Dehenney' , 'Washington' /)

In this constructor, without the type specification, Intel® Fortran makes all of the array elements length 10, that is, as long as the longest character element.

Parent topic: Arrays

## See Also

Execution Control
Subscript triplets
Derived types
Structure constructors
Array Elements for details on array element order
Array Assignment Statements for details on another way to assign values to arrays
Declaration Statements for Arrays for details on array specifications