next up previous contents index
Next: Character Arrays Up: Arrays as Arguments Previous: Assumed-size Arrays

Array Sections

The rules of Fortran require that the extent of an array (in each dimension if it is multi-dimensional) must be at least as large in the actual argument as in the dummy argument, but they do not require actual agreement of both lower and upper bounds. For example:

 
       PROGRAM CONFUS 
       REAL X(-1:50), Y(10:1000) 
       READ(UNIT=*,FMT=*) X, Y 
       CALL OUTPUT(X) 
       CALL OUTPUT(Y) 
       END 

       SUBROUTINE OUTPUT(ARRAY) 
       REAL ARRAY(50) 
       WRITE(UNIT=*,FMT=*) ARRAY 
       END
The effect of this program will be to output the elements X(-1) to X(48) since X(48) corresponds to ARRAY(50), and then output Y(10) to Y(59) also. The subroutine will work similarly on a slice through a two-dimensional array:
 
       PROGRAM TWODIM 
       REAL D(100,20) 
* ... 
       NSLICE = 15 
       CALL OUTPUT(D(1,NSLICE))
In this example the slice of the array from elements D(1,15) to D(50,15) will be written to the output file. In order to work out what is going to happen you need to know that Fortran arrays are stored with the first subscript most rapidly varying, and that the argument association operates as if the address of the specified element were transferred to the base address of the dummy argument array.

The use of an array element as an actual argument when the dummy argument is a complete array is a very misleading notation and the transfer of array sections should be avoided if at all possible.



Mario Storti
Wed Nov 4 19:32:56 ART 1998