I have been writing a large Fortran program for thermodynamic calculations for almost 10 years and when I started I was new to the new Fortran standard (I was familiar with F77 and too old to learn something else). I found the new TYPE constructs very nice and have used them frequently but I was not aware of some limitations, such as it was not allowed to create arrays of pointers, I have discovered that later.
Now I am correcting some of my old code and I am surprised to find inside a record declaration for: TYPE gtp_phase_add
a declaration: TYPE(tpfun_expression), dimension(:), pointer :: explink
where explink is used to point to another structure containing a mathematical expression. This has not generated any compilation errors (I normally use gfortran but I have complied this program with intel fortran also). When I saw this old code (written some 10 year ago) I thought there is an "allocatable" missing but adding that gave a compilation error.
I made a minimal complete program to mimic how this is used:
MODULE test1
implicit none
TYPE tpfun_expression
integer nc
double precision, allocatable, dimension(:) :: coeffs
integer, allocatable, dimension(:) :: powers
END type tpfun_expression
TYPE gtp_phase_add
!**************************************************************************
! My question is if it is correct Fortran to have an array of pointers here
TYPE(tpfun_expression), dimension(:), pointer :: explink
!**************************************************************************
TYPE(gtp_phase_add), pointer :: nextadd
END TYPE gtp_phase_add
contains
subroutine create_tpfun(n,coeffs,powers,exp)
integer n,i,powers(*)
double precision coeffs(*)
type(tpfun_expression), pointer :: exp
allocate(exp%coeffs(n))
allocate(exp%powers(n))
exp%nc=n
do i=1,n
exp%coeffs(i)=coeffs(i)
exp%powers(i)=powers(i)
enddo
return
end subroutine create_tpfun
subroutine create_addrec(typ,this)
integer typ,n,m
TYPE(tpfun_expression), target :: exp1
TYPE(tpfun_expression), pointer :: exp2
TYPE(gtp_phase_add), pointer :: this
integer ipow(4)
double precision coeffs(4)
!
!**************************************************************************
! here I allocate a pointer array
allocate(this%explink(typ))
!**************************************************************************
if(typ.eq.1) then
do m=1,4
ipow(m)=m-1
coeffs(m)=2.0D0*m
enddo
exp2=>this%explink(1)
call create_tpfun(4,coeffs,ipow,exp2)
else
do m=1,4
ipow(m)=m-1
coeffs(m)=3.0D0
enddo
exp2=>this%explink(1)
call create_tpfun(4,coeffs,ipow,exp2)
do m=1,3
ipow(m)=1-m
coeffs(m)=5.0D0
enddo
exp2=>this%explink(2)
call create_tpfun(3,coeffs,ipow,exp2)
endif
return
end subroutine create_addrec
end MODULE test1
program main
use test1
integer n,m,j,k,q
TYPE(gtp_phase_add), target :: addrec
TYPE(gtp_phase_add), pointer :: next,first
TYPE(tpfun_expression) :: exp
first=>addrec
next=>addrec
write(*,*)'Creating addrec 1'
call create_addrec(1,next)
allocate(next%nextadd)
write(*,*)'Creating addrec 2'
next=>next%nextadd
call create_addrec(2,next)
! just a check that the functions are correct
write(*,*)'Listing functions in all addrecs'
next=>first
q=0
do while(associated(next))
q=q+1
write(*,*)'Addition record ',q
n=size(next%explink)
do m=1,n
k=next%explink(m)%nc
write(*,10)(next%explink(m)%coeffs(j),next%explink(m)%powers(j),j=1,k)
enddo
10 format(10(F6.3,1x,i3))
next=>next%nextadd
enddo
end program main
This works as I expect, I am only surprised that I allowed to declare an array of pointers so I would like to know if this is correct Fortran.' And sorry if I am not able to understand how to edit this more elegantly.