Silverfrost Forums

Welcome to our forums

Multiple rank part references

27 Jan 2021 12:07 #26977

Imagine a derived-type array, and the derived type itself contains an array. Is it possible to reference the data using Fortran's array notation for both arrays? I am not quite sure how to ask the question properly, but my example below hopefully illustrates what I mean. The second executable line is commented out because it generates a compilation error. If the syntax were valid, a subsequent question would be how to use such syntax in a subroutine call? For example, would the last (commented) line before Contains, be considered reasonable - could we not interpret the resulting argument as a two-dimensional array?

Program p
   Integer, Parameter :: n = 3
   Type t
     Integer, Dimension(n) :: i
   End Type t
   Type(t) :: t1
   Type(t), Dimension(n) :: t2
!
   t1%i(:) = 1
!   t2(:)%i(:) = 1 ! What is valid syntax for this line?
   t2(:)%i(1) = 2
   t2(:)%i(2) = 2
   t2(:)%i(3) = 2
   Call s1 (t1%i(:))
   Call s2 (t2(:)%i(1))
!   Call s3 (t2(:)%i(:)) ! How could a subroutine be called?
!
Contains
 Subroutine s1 (i)
   Integer, Dimension(:), Intent(In) :: i
   Print*, 's1', i
 End Subroutine s1
 Subroutine s2 (i)
   Integer, Dimension(:), Intent(In) :: i
   Print*, 's2', i
 End Subroutine s2
 Subroutine s3 (i)
   Integer, Dimension(:,:), Intent(In) :: i
   Print*, 's3', i
 End Subroutine s3
End Program p

Incidentally, I came across the following page while trying to investigate this issue, but I do not know what the outcome was: https://fortran.bcs.org/2005/newreqs/multi_part_refs.txt

27 Jan 2021 2:59 #26982

Simon

You can write

t2(1)%i = v

or

t2%i(1) = v

but you can't do both together.

If you want to pass the whole lot then I guess the call would be

CALL s3(t2)

where s3 would have the corresponding interface.

Please login to reply.