Hi there,
I've got a bit of code that I've been given to work on. It uses a structure constructor to initialise a derived type, but it uses a function within the constructor to set a pointer.
Is it legal to do this ? I'm getting different results on different compilers (Intel's compiler is OK with it, but PGI's fails with a very useless message).
Here's a simple test code showing the case:
MODULE Test
! Using a function to nullify or associate pointer
! in constructor of a simple derived type
implicit none
type deriv
real(kind=8), dimension), pointer :: ptr
end type deriv
real(kind=8),dimension),pointer:
contains
subroutine foo()
integer(kind=4) :: i=1
type(deriv) , pointer :: derivptr
! Structure constructor fails with function inside
derivptr = deriv(pnull(p,i))
end subroutine foo
function pnull(pn,i)
! Returns pointer to pn or NULL, depending on value of i
integer(kind=4), intent(in) :: i
real(kind=8),dimension),pointer :: pn
real(kind=8),dimension),pointer :: pnull
nullify(pnull)
if (i > 0) pnull=>pn
end function pnull
END MODULE Test
I've got a bit of code that I've been given to work on. It uses a structure constructor to initialise a derived type, but it uses a function within the constructor to set a pointer.
Is it legal to do this ? I'm getting different results on different compilers (Intel's compiler is OK with it, but PGI's fails with a very useless message).
Here's a simple test code showing the case:
MODULE Test
! Using a function to nullify or associate pointer
! in constructor of a simple derived type
implicit none
type deriv
real(kind=8), dimension), pointer :: ptr
end type deriv
real(kind=8),dimension),pointer:
contains
subroutine foo()
integer(kind=4) :: i=1
type(deriv) , pointer :: derivptr
! Structure constructor fails with function inside
derivptr = deriv(pnull(p,i))
end subroutine foo
function pnull(pn,i)
! Returns pointer to pn or NULL, depending on value of i
integer(kind=4), intent(in) :: i
real(kind=8),dimension),pointer :: pn
real(kind=8),dimension),pointer :: pnull
nullify(pnull)
if (i > 0) pnull=>pn
end function pnull
END MODULE Test