-2

I want to have a small subroutine to make a duplicated copy of a pointer, which is already allocated /defined. I have the following test code:

implicit none
real ,pointer :: x(:),y(:)
real,target:: px
px=2.02
allocate(x(10))
x=20.0
call copy_1dptr(x,y)  !code aborting
write(6,*)'size x is ',size(x)
write(6,*)'y is ',y
end
subroutine copy_1dptr(x,y) 
real,pointer:: x(:),y(:)
allocate(y(size(x)))
y=x
end subroutine copy_1dptr

However, (using ifort), when I ran it, I always got the aborting error, with message saying:

forrtl: severe (408): fort: (7): Attempt to use pointer Y when it is not associated with a target

Seems, the null pointer can't be used as an actual dummy like what I tried to do. Is that true? Are there any solution to this.

Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
Ting Lei
  • 91
  • 9

1 Answers1

2

Pointer arguments require explicit interface. Place the subroutine in a module or make it internal:

contains

  subroutine copy_1dptr(x,y) 
  end subroutine

end program