Reputation: 182
Is it possible to make the dimension of the array allocatable? (not just the size of a dimension)
i.e., something giving:
REAL, DIMENSION(:,:,: ... n), ALLOCATABLE :: array
I mean this in an array of arrays sense, but can we do it preserving Fortran's easily accessible array structure? There was this, but the first answer does not satisfy this need. The second answer uses pointers. Will that work?
Upvotes: 1
Views: 183
Reputation: 1334
Is it possible to make the dimension of the array allocatable?
Yes,Fortran 2018 standard specifies the syntax for this: e.g.,
real :: a(..)
However, an assumed rank object must be a DUMMY argument, which means that it is not a new type of array that you can create in your main program, it just refers to an actual array you passed in. For example:
REAL :: a0
REAL :: a1(10)
REAL :: a2(10, 20)
REAL,allocatable :: a3(:,:,:)
CALL sub1(a0)
CALL sub1(a1)
CALL sub1(a2)
CALL sub1(a3)
CONTAINS
SUBROUTINE sub1(a)
REAL :: a(..)
PRINT *, RANK(a)
END
END
It will output:
0
1
2
3
Upvotes: 0
Reputation: 18118
No, it is not possible to have an array with variable rank. From the Fortran 2008 Standard, Cl. 2.4.6 "Array":
1 An array may have up to fifteen dimensions, and any extent in any dimension. The size of an array is the total number of elements, which is equal to the product of the extents. An array may have zero size. The shape of an array is determined by its rank and its extent in each dimension, and is represented as a rank-one array whose elements are the extents. All named arrays shall be declared, and the rank of a named array is specified in its declaration. The rank of a named array, once declared, is constant; the extents may be constant or may vary during execution.
[Emphasis mine.]
However, you could have a one-dimensional array with extent product(extent in each dimension)
, and index the elements appropriately.
You could even have multi-dimensional pointers associated with these 1D-arrays. This would take care of the indexing for you, but (as given in the citation), is limited to 15 dimensions for Standard Fortran.
Upvotes: 4