Assumed-size arrays

Assumed-size arrays are dummy argument arrays where the size is inherited from the associated actual argument, but the rank and extents might differ.

Assumed_size_spec

Read syntax diagramSkip visual syntax diagram
>>-+----------------------------------------+--+----------------+--*-><
   | .-,-------------------------------.    |  '-lower_bound--:-'      
   | V                                 |    |                          
   '---+----------------+--upper_bound-+--,-'                          
       '-lower_bound--:-'                                              

lower_bound, upper_bound
are specification expressions

If any bound is not constant, the array must be declared inside a subprogram and the nonconstant bounds are determined on entry to the subprogram. If a lower bound is omitted, its default value is 1.

The last dimension has no upper bound and is designated instead by an asterisk. You must ensure that references to elements do not go past the end of the actual array.

The rank equals one plus the number of upper_bound specifications in its declaration, which may be different from the rank of the actual array it is associated with.

The size is assumed from the actual argument that is associated with the assumed-size array:
  • TS 29113 beginsIf the actual argument is a scalar and the assumed-size dummy argument is of assumed-type, the assumed-size array has exactly one element.TS 29113 ends
  • If the actual argument is a noncharacter array, the size of the assumed-size array is that of the actual array.
  • If the actual argument is an array element from a noncharacter array, and if the size remaining in the array beginning at this element is S, then the size of the dummy argument array is S. Array elements are processed in array element order.
  • If the actual argument is a character array, array element, or array element substring, and assuming that:
    • A is the starting offset, in characters, into the character array
    • T is the total length, in characters, of the original array
    • S is the length, in characters, of an element in the dummy argument array
    then the size of the dummy argument array is:

    MAX( INT (T - A + 1) / S, 0 )

    For example:
    CHARACTER(10) A(10)
    CHARACTER(1) B(30)
    CALL SUB1(A)          ! Size of dummy argument array is 10
    CALL SUB1(A(4))       ! Size of dummy argument array is 7
    CALL SUB1(A(6)(5:10)) ! Size of dummy argument array is 4 because there
                          ! are just under 4 elements remaining in A
    CALL SUB1(B(12))      ! Size of dummy argument array is 1, because the
                          ! remainder of B can hold just one CHARACTER(10)
    END                   ! element.
    SUBROUTINE SUB1(ARRAY)
      CHARACTER(10) ARRAY(*)
      …
    END SUBROUTINE

Examples

Example 1
INTEGER X(3,2)
DO I = 1,3
   DO J = 1,2
      X(I,J) = I * J      ! The elements of X are 1, 2, 3, 2, 4, 6
   END DO
END DO
PRINT *,SHAPE(X)          ! The shape is (/ 3, 2 /)
PRINT *,X(1,:)            ! The first row is (/ 1, 2 /)
CALL SUB1(X)
CALL SUB2(X)
END
SUBROUTINE SUB1(Y)
  INTEGER Y(2,*)          ! The dimensions of y are the reverse of x above
  PRINT *, SIZE(Y,1)      ! We can examine the size of the first dimension
                          ! but not the last one.
  PRINT *, Y(:,1)         ! We can print out vectors from the first
  PRINT *, Y(:,2)         ! dimension, but not the last one.
END SUBROUTINE
SUBROUTINE SUB2(Y)
  INTEGER Y(*)            ! Y has a different rank than X above.
  PRINT *, Y(6)           ! We have to know (or compute) the position of
                          ! the last element.  Nothing prevents us from
                          ! subscripting beyond the end.
END SUBROUTINE
TS 29113 begins
Example 2
REAL r1
CALL sub(r1)
!You can pass a scalar as the actual argument that corresponds 
!to an assumed-size dummy argument of assumed-type.
CONTAINS
  SUBROUTINE sub(arg)
    TYPE(*) :: arg(3, 2:*)
  END
END
TS 29113 ends
Notes:
  1. An assumed-size array cannot be used as a whole array in an executable construct unless it is an actual argument in a subprogram reference that does not require the shape:
    ! A is an assumed-size array.
    PRINT *, UBOUND(A,1) ! OK - only examines upper bound of first dimension.
    PRINT *, LBOUND(A)   ! OK - only examines lower bound of each dimension.
    ! However, 'B=UBOUND(A)' or 'A=5' would reference the upper bound of
    ! the last dimension and are not allowed.  SIZE(A) and SHAPE(A) are
    ! also not allowed.
  2. If a section of an assumed-size array has a subscript triplet as its last section subscript, the upper bound must be specified. (Array sections and subscript triplets are explained in a subsequent section.)
    ! A is a 2-dimensional assumed-size array
    PRINT *, A(:, 6)       ! Triplet with no upper bound is not last dimension.
    PRINT *, A(1, 1:10)    ! Triplet in last dimension has upper bound of 10.
    PRINT *, A(5, 5:9:2)   ! Triplet in last dimension has upper bound of 9.


Voice your opinion on getting help information Ask IBM compiler experts a technical question in the IBM XL compilers forum Reach out to us