fortran: how to pass a null pointer to a subroutine, in which it will be defined and returned

1.5k views Asked by At

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.

1

There are 1 answers

0
Vladimir F Героям слава On BEST ANSWER

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