How would I pass as an input to a subroutine a field of a derived data type?
I have a linked list that I want to loop trough and I want to work on a specific field only, but I would like to have the field "name" as input so that I can call the same function on different fields of the list.
For instance:
program main
implicit none
! ----- variables declaration
type :: element
real :: u1
real :: u2
type (element), pointer :: next => null()
end type element
type (element), pointer :: first, last, iele
! ----- code
allocate( first )
last => first
first %u1 = 0
first %u2 = 0
allocate( first %next )
last => first %next
last %u1 = 10
last %u2 = 20
call addten( u1, first )
call addten( u2, first )
iele => first
do while ( associated( iele ) )
write(*,*) iele %u1
iele => iele %next
end do
end program main
! =====
subroutine addten( u, first )
implicit none
! ----- variables declaration
type :: element
real :: u1
real :: u2
type (element), pointer :: next => null()
end type element
real, pointer :: u
type (element), pointer :: iele
! ----- code
iele => first
do while ( associated( iele ) )
iele %u = iele %u + 10
iele => iele %next
end do
end subroutine addten