Pavan Yalamanchili
Pavan Yalamanchili

Reputation: 12109

Indexing user defined types in fortran

In short, can I do something like this in fortran (any of the fortran standards)

type(my_array), dimension(:,:), allocatable :: a
type(my_array), dimension(5,5) :: b
allocate(a(3, 3))
a = b(1:3, 1:3)

Upvotes: 2

Views: 230

Answers (1)

sverre
sverre

Reputation: 6919

Yes, of course you can (in f90+).

$ cat foo.f90
program foo
implicit none

type :: my_array
  integer :: i
end type my_array

type(my_array), dimension(:,:), allocatable :: a
type(my_array), dimension(5,5) :: b
integer :: i, j

do i = 1, 5
  do j = 1, 5
    b(j,i)%i = 10*i + j
  end do
end do

allocate(a(3, 3))
a = b(1:3, 1:3)

write(*,"(3i3)") a

end program foo
$ gfortran foo.f90 -o foo
$ ./foo
 11 12 13
 21 22 23
 31 32 33
$ 

Upvotes: 5

Related Questions