小编Geo*_*ffH的帖子

Fortran 接口调用返回指向数组的指针的 C 函数

经过多次搜索,我发现我认为最接近我的问题的答案是在Fortran 接口的Stack Overflow (SO) 上调用返回指针的 C 函数,(近 10 年前发布!)

我引用这一点是因为使用该示例可以使代码保持简单并仍能说明我的问题。

我想返回一个已在 C++ 中创建/分配的内存的数组,并能够在 Fortran 中分析答案,因为这是该应用程序的大部分代码所在。我的应用程序进入 C++ 以生成整数数组答案,并通过 C 接口将其返回给 Fortran 程序。原始 SO 示例使用单个双精度变量作为返回值。我已将其更改为整数,因为这是我将在我的应用程序中处理的内容。示例代码(已更改)有效。

我在评论中强调了我试图为返回数组指针所做的更改,但我已经没有想法了。(我可以说,“哦,对于那些糟糕的过去,我可以将一个整数等同于一个 iarray(1) 并超出数组的大小”,但我不会。有编码保护是很好的,但有时它变得令人沮丧。)

我正在使用 Visual Studio 2017 和 Intel Fortran parallel_studio_xe_2019_update5_composer。

我对原始 SO 代码的修改示例:

! ps_test_pointers.f90

program foo
  use, intrinsic :: iso_c_binding, only : c_ptr,        &
                                          c_f_pointer,  &
                                          c_int
  implicit none
  type(c_ptr) :: c_p!(:) ! <-------
  integer(c_int), pointer :: f_p!(:) ! <-------

  interface
    function foofunc() bind(c)
      import :: c_ptr
      implicit none  
      type(c_ptr) :: …
Run Code Online (Sandbox Code Playgroud)

arrays fortran pointers interface fortran-iso-c-binding

3
推荐指数
1
解决办法
363
查看次数