The correct solution to this depends on the behavior you desire. Do you want the pointer to remember what it points to between calls, or should it begin each invocation anew? Beware, if you give it the save attribute (which => null() does implicitly), do not point at anything that does not also have the save attribute. For example, the following will crash
program bad_idea
implicit none
call bar()
call bar()
contains
subroutine foo(bad)
integer, intent(in), target :: bad
integer, pointer, save :: a => null()
logical, save :: first = .true.
if (first) then
a => bad
end
print *, bad
end subroutine
subroutine bar
integer, target :: x
x = 42
call foo(x)
end subroutine
end program
because when x goes out of scope, it disappears and thus a will remain pointing at something that doesn’t exist. Note that the associated intrinsic will not save you in this case either, because the pointer is associated with something, it’s just random memory. A simpler example that suffers from the same problem
program bad_idea
implicit none
call foo()
call foo()
contains
subroutine foo()
integer, target :: bad
integer, pointer, save :: a => null()
logical, save :: first = .true.
bad = 42
if (first) then
a => bad
end
print *, bad
end subroutine
end program