Because of some reason, I need to pass a Fortran pointer to a subroutine. The subroutine is inside a module and the main program uses this module to ensure explicit interface.
My question is, what attribute should I specify on the dummy argument of the subroutine in order to receive the passed-in pointer?
I tried the code below.
module aaa
contains
integer*4 function print_ptr_arr_1( ptr )
implicit none
integer*4, intent(in), pointer :: ptr(:)
print *, 'as pointer'
print *, size(ptr)
print '(10i3)', ptr
print *
end function print_ptr_arr_1
integer*4 function print_ptr_arr_2( ptr )
implicit none
integer*4, intent(in), target :: ptr(:)
print *, 'as target'
print *, size(ptr)
print '(10i3)', ptr
print *
end function print_ptr_arr_2
integer*4 function print_ptr_arr_3( ptr )
implicit none
integer*4, intent(in) :: ptr(:)
print *, 'as assumed shape array'
print *, size(ptr)
print '(10i3)', ptr
print *
end function print_ptr_arr_3
end module aaa
and
program main
use aaa
implicit none
integer*4 :: i1, ierr
integer*4, target :: arr(10)
integer*4, pointer :: ptr_arr(:)
do i1 = 1, 10
arr(i1) = i1
end do
ptr_arr => arr
ierr = print_ptr_arr_1( ptr_arr )
ierr = print_ptr_arr_2( ptr_arr )
ierr = print_ptr_arr_3( ptr_arr )
nullify( ptr_arr )
end program main
Three subroutines in module aaa show correct outputs as below.
sj2734@sonaram:~/work/practice/fortran_pointer$ ./a.out
as pointer
10
1 2 3 4 5 6 7 8 9 10
as target
10
1 2 3 4 5 6 7 8 9 10
as assumed shape array
10
1 2 3 4 5 6 7 8 9 10
sj2734@sonaram:~/work/practice/fortran_pointer$
What is correct and what are not? Or, are these all correct?