使用指针算法将C函数转换为Fortran

时间:2017-11-25 04:34:31

标签: c pointers fortran

我们正在将C函数列表转换为Fortran。这是出于学术目的,我们没有关于整个C程序的详细信息。我们的目标是将C函数转换为Fortran而不会出现任何编译器错误。我的C函数如下

barheaponarray(double *space, int *np, int *szp) {

  void *vspace, *oldvspace;
  int diff;
  heap *h;
  heapnode *d;
  int n, sz;

   n = *np; sz = *szp;

  vspace = (void *) &space[n-1];
  oldvspace = vspace;

  diff = (char *) vspace - (char *) oldvspace;
  vspace = (void *) ((char *) oldvspace + 1;
  h = (heap *)vspace;
  vspace = (void *)(h + 1);

  diff = (char *) vspace - (char *) oldvspace;
  vspace = (void *) ((char *) oldvspace +1;
  d = (heapnode *)vspace;
  vspace = (void *)(d + sz); 

  diff = (char *) vspace - (char *) oldvspace;
  vspace = (void *) ((char *) oldvspace + 1;

  *np = (double *)vspace - space + 1;
   return (n);      /* note unchanged from beginning */

}

这里的heap和heapnode是C结构

typedef struct heapnode {
          double c;
          int n;
                } heapnode;

 typedef struct heap {
            int n;
            int sz;
            heapnode *d;
                } heap;

我将上面的函数转换为Fortran,如下所示。它会在下面提供编译器警告

module typesDef

  type heapnode

    real*8 :: c
    integer*4 :: n

   endtype heapnode


  type heap

    integer*4 :: n
    integer*4 :: sz
    type(heapnode), pointer :: d

  endtype heap

end module

integer*4 function  barheaponarray(space, np, szp)

   use typesDef

   implicit none

   real*8, pointer :: vspace, oldvspace

   real*8, target  :: space(1)
   integer*4 :: np, szp
   integer*4 :: n, sz, diff

   integer*4 :: baralign !to call function baralign


   type(heap), pointer :: h
   type(heapnode), pointer :: d

   n = np
   sz = szp

   vspace => space(n)
   oldvspace = vspace

   diff = vspace - oldvspace
   vspace = oldvspace + 1
   h = TRANSFER(vspace,mold=heap)
   vspace = h + 1

   diff = vspace -  oldvspace
   vspace = oldvspace + 1)
   d = TRANSFER(vspace,mold=heapnode)
   vspace = d + sz

   diff = vspace - oldvspace
   vspace = oldvspace + 1

   np = vspace - space + 1

   barheaponarray = n

   return

endfunction barheaponarray

这里我不确定数据类型转换,特别是在Fortran中使用void指针。

编译器在行

中进行以下警告
 diff = vspace - oldvspace

Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) 

如何正确转换此代码?

0 个答案:

没有答案