Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing pointer component of derived type into subroutine

Status
Not open for further replies.

narlytep

Programmer
Apr 18, 2006
10
GB
Hi.
One of our users is having a problem and I'm a bit stumped as well. They're trying to pass a pointer component of a derived type into a subroutine and treating it as a normal array at the other end. However the data is ending up as garbage.

Is this legal?

module mod
type deriv
integer :: dummy
real(kind=8), dimension:)), pointer :: p
end type deriv
type(deriv) :: struct
end module mod

subroutine sub1()
use mod
implicit none

interface
subroutine sub2(t)
real(kind=8), dimension(0:25), intent(in) :: t
end subroutine sub2
end interface

allocate(struct%p(0:25))
! Set values of struct%p here........
!
call sub2(struct%p)
end subroutine sub1

subroutine sub2(t)
real(kind=8), dimension(0:25), intent(in) :: t
print*, t(0), t(1), t(2)
end subroutine sub2

-----------------------------------
Although struct%p contains the value 5.0 throughout (apart from index 0 which holds 0.0), the t array in sub2 holds:

3.2e-324 <denormalised>
3.2e-324 <denormalised>
0
3.2e-324 <denormalised>
3.2e-324 <denormalised>
0
etc.

The memory address for struct%p and t are identical.

Any help appreciated!
 
It shouldn't be a pointer. Try these mods
Code:
module mod
  type deriv
     integer :: dummy
[COLOR=red]     real(kind=8), dimension(:) :: p[/color]
  end type deriv
  type(deriv) :: struct
end module mod

subroutine sub1()
  use mod
  implicit none

  interface
    subroutine sub2(t)
[COLOR=red]      real(kind=8), dimension(:), intent(in) :: t[/color]
    end subroutine sub2
  end interface

  allocate(struct%p(0:25))
! Set values of struct%p here........
!
  call sub2(struct%p)
end subroutine sub1

subroutine sub2(t)
[COLOR=red]  real(kind=8), dimension(:), intent(in) :: t[/color]
  print*, t(0), t(1), t(2)
end subroutine sub2
 
could you explain more about the difference in using pointer and array in fortran?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top