Fortran と関数/サブルーチン ポインターに問題があります。引数として配列を取る 2 つの関数があります。f1 では a(n,n)、f2 では a(n*n) です。サブルーチンを手動で呼び出すと、同じ配列でこれを行うことができます。
real :: a(5, 5)
call f1(a, 5)
call f2(a, 5)
しかし、ポインターを使用してこれを実行しようとすると、コンパイラーは次のエラーでそれをスローします。
ptr => f2
1
Error: Interface mismatch in procedure pointer assignment at (1): Type/rank missmatch in argument 'a'
これを回避する方法はありますか?ポインターについて考えていましたが、同じ問題があり、作成するには次元数を知る必要があります。
参考までに、ここに完全なコードを示します (長すぎないことを願っています..)
program ptrtest
implicit none
interface
subroutine fnc(a, n)
integer :: n
real :: a(n, n)
end subroutine
subroutine f1(a, n)
integer :: n
real :: a(n, n)
end subroutine
subroutine f2(a, n)
integer :: n
real :: a(n*n)
end subroutine
end interface
procedure(fnc), pointer :: ptr => null()
real :: a(5, 5)
real :: b(4, 4)
ptr => f1
call ptr(a, 5)
write(*,*) a
!this one does not work..
!ptr => f2
!
!call ptr(b, 4)
!write(*,*) b
call f2(b, 4)
write(*,*) b
end program
subroutine f1(a, n)
integer :: n
real :: a(n, n)
integer :: i
a = 1
end subroutine
subroutine f2(a, n)
integer :: n
real :: a(n*n)
a = 2
end subroutine
これを行う方法があることを本当に願っています。配列の次元が毎回一致するように、すべてのサブルーチンを実際に書き直すことはできません:/
よろしく、カバ