如何使用iso_c_binding声明指针? [英] How to declare pointer-of-pointer using iso_c_binding?

查看:82
本文介绍了如何使用iso_c_binding声明指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Fortran编写 iso_c_binding 来调用具有以下原型的C函数

I am writing an iso_c_binding in Fortran to call a C-function with the below prototype

int zmat_run(
    const size_t inputsize,
    unsigned char *inputstr, 
    size_t *outputsize, 
    unsigned char **outputbuf,
    const int zipid, 
    int *ret, 
    const int iscompress
);

我的问题是如何在此接口中声明 unsigned char ** outputbuf ,这是在c函数内部用于分配输出缓冲区的指针?

My question is how do I declare unsigned char **outputbuf, a pointer that is used inside the c-function to allocate output buffer, in this interface?

此外,我应该在Fortran中使用哪种数据类型作为传递给此 outputbuf 参数的真实参数?应该可以分配吗?(如果它是在c函数内部分配的)?

Also, what data type I should be using in Fortran as the real parameter to pass to this outputbuf parameter? should it be allocatable? (if it is allocated inside the c-function)?

我目前正在起草该模块,但尚未对其进行测试(我怀疑它会起作用).

I currently drafted this module, but haven't tested it (I doubt it will work).

module zmatlib
  use iso_c_binding, only: c_char, c_size_t, c_ptr, C_NULL_CHAR
  interface
    integer(c_int) function zmat_run(inputsize, inputbuf, outputsize, outputbuf, zipid, ret, level) bind(C, name="zmat_run")
      use iso_c_binding
      integer(c_size_t), value :: inputsize
      integer(c_int), value :: zipid, level
      integer(c_size_t),  intent(out) :: outputsize
      integer(c_int),  intent(out) :: ret
      character(kind=c_char),  intent(in)  :: inputbuf(*)
      character pointer(c_ptr),intent(out) :: outputbuf
    end function zmat_run
  end interface
end module

推荐答案

尝试类型(C_PTR),意图(输出).然后,您将需要使用Fortran函数 c_f_pointer 将C指针与Fortran指针关联.可能是 C_CHAR 类型.

Try type (C_PTR), intent (out). Then you will need to use Fortran function c_f_pointer to associate the C pointer with a Fortran pointer. Probably of type C_CHAR.

这篇关于如何使用iso_c_binding声明指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆