2

それは私の最初の質問投稿です... 残酷にならないでください。

私の問題は次のとおりです。Fortran ポインターを式として代入したいと思います。単純な fortran の手法では不可能だと思います。しかし、新しいバージョンの fortran では、C および C++ で使用されるもの (c_ptr や c_f_pointer ... など) を処理する方法が提供されているように見えるため、誰かが私の問題を解決する方法を知っている可能性があります。(私は C についてあまり考えていませんが、C ではポインター演算が可能であることを読みました) より明確にするために、すぐに頭に浮かんだが機能しないコードを次に示します。

program pointer

real(8),target :: a
real(8),pointer :: b

b=>a*2.0d0  ! b=>a is of course working

do i=1,10
   a=dble(i)*2.0d0
   write(*,*)b
end do

end program

この問題を回避する方法があることは知っていますが、実際のプログラムでは、それらすべてが頭に浮かび、計算時間が大幅に長くなったり、コードが非常に複雑になったりする可能性があります。

よろしくお願いします!

ベスト、ピーター

4

4 に答える 4

1

マイケル・メトカーフから、

ポインターは、POINTER 属性を持つ変数です。それらは別個のデータ型ではありません (したがって、「ポインター演算」は不可能です)。

これらは、概念的には、ポインターが指す可能性のあるオブジェクト (ターゲット) の属性と、ターゲットのアドレス (存在する場合) をリストする記述子です。割り当てられるか、関連付けられるまで、関連付けられたストレージはありません (ポインターの割り当てによって、以下を参照)。

割り当てられており、 のが与えられてb=>a*2いないため、あなたの考えは機能しません。baa

于 2013-10-17T20:03:46.093 に答える
1

Expression, in general (there two and a half very significant exceptions), are not valid pointer targets. Evaluation of an expression (in general) yields a value, not an object.

(The exceptions relate to the case where the overall expression results in a reference to a function with a data pointer result - in that case the expression can be used on the right hand side of a pointer assignment statement, or as the actual argument in a procedure reference that correspond to a pointer dummy argument or [perhaps - and F2008 only] in any context where a variable might be required, such as the left hand side of an ordinary assignment statement. But your expressions do not result in such a function reference and I don't think the use cases are relevant to what you wnt to do. )

I think you want the value of b to change as the "underlying" value of a changes, as per the form of the initial expression. Beyond the valid pointer target issue, this requires behaviour contrary to one of the basic principles of the language (most languages really) - evaluation of an expression uses the value of its primaries at the time the expression is evaluation - subsequent changes in those primaries do not result in a change in the historically evaluated value.

Instead, consider writing a function that calculates b based on a.

program pointer
  IMPLICIT NONE
  real(8) :: a
  do i=1,10
    a=dble(i)*2.0d0
    write(*,*) b(a)
  end do
contains
  function b(x)
    real(kind(a)), intent(in) :: x
    real(kind(a)) :: b
    b = 2.0d0 * x
  end function b
end program
于 2013-10-17T20:34:17.317 に答える
1

更新:私が欲しかったものに近づいています(興味のある人向け):

module test

real,target :: a
real, pointer :: c

abstract interface
   function func()
      real :: func
   end function func
end interface

procedure (func), pointer :: f => null ()

contains

function f1()
  real,target :: f1

  c=>a
  f1 = 2.0*c

  return
end function f1

end module

program test_func_ptrs

use test

implicit none

integer::i

f=>f1

do i=1,10
   a=real(i)*2.0
   write(*,*)f()
end do

end program test_func_ptrs

仮引数を回避する方法を見つけることができれば、私は完全に満足するでしょう (少なくとも f を呼び出すとき)。

追加情報:ポイントは、ループを開始する前に、さまざまな関数 f1 を定義し、deside を定義したいということです。ループ内で f がどのようなものになるか (入力に応じて)。

于 2013-10-17T22:12:33.540 に答える
0

ポインターからアドレス オフセットを計算するという意味でのポインター演算は、Fortran では許可されていません。ポインタ演算は簡単にメモリ エラーを引き起こす可能性があり、Fortran の作成者はそれが不要であると考えていました。(C との相互運用性のバックドアを介して行うことができます。)

Fortran のポインターは、プロシージャーを引数として渡したり、連結リストなどのデータ構造を設定したりするのに役立ちます (例: Fortran 2003-2008 で連結リストを実装するにはどうすればよいですか)。

于 2013-10-17T20:16:42.437 に答える