传递Fortran数组到C [英] Passing Fortran arrays to C

查看:425
本文介绍了传递Fortran数组到C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多的Fortran传递阵列与C程序的麻烦。从我从previous帖子聚集是接口的包容性。这摆脱了我的一些问题。不过,我似乎无法弄清楚如何正确地传递这些阵列或正确地访问内部C.其值

I am having a lot of trouble passing Fortran arrays to a C program. From what I have gathered from previous posts is the inclusion of the interface. That got rid of some of my problems. However, I cannot seem to figure out how to pass these arrays properly or access their values correctly inside C.

program f_call_c
  implicit none


  interface
     subroutine cfun(x,len) bind( c )
       use,intrinsic :: iso_c_binding
       implicit none
       integer( c_int) :: len
       real(c_double) :: x(0:3,0:len)
     end subroutine   cfun

     subroutine vec(r,len) bind(c)
       use,intrinsic :: iso_c_binding
       implicit none
       integer(c_int) :: len
       real(c_double) :: r(0:len)
     end subroutine vec


  end interface


  double precision, allocatable :: x(:,:),r(:)
  integer :: len,row,i,j

  len = 7
  allocate(x(0:3,0:len))
  allocate(r(0:len))

  do i =0,len
     r(i) = i
  enddo


  do i = 0,len
     do j = 0,3
        x(j,i) = j+i
     enddo
  enddo

  call vec(r,%val(len) )

  row = 3
  call cfun(x,%val(len))
end program f_call_c


#include <stdio.h>
void cfun(double **x,const int len)
{
  printf("%d\n", len);
  printf("This is in C function cfun...\n");


  for(int i=0; i<len; i++)
    {
      printf(" %d\n  %d\n  %d\n", x[0][i]);

    }
}

void vec(  double *r[],const int len )
{
  printf("This is in C function vec...\n");

  printf("%d\n", len);

  for(int i=0; i<len; i++)
    {
       printf(" %d\n", r[i]);

    }

}

目前,输出

    Fortran calling C, passing
 r at           0 is   0.0000000000000000     
 r at           1 is   1.0000000000000000     
 r at           2 is   2.0000000000000000     
 r at           3 is   3.0000000000000000     
 r at           4 is   4.0000000000000000     
 r at           5 is   5.0000000000000000     
 r at           6 is   6.0000000000000000     
 r at           7 is   7.0000000000000000     
This is in C function vec...
7
 0
 0
 0
 0
 0
 0
 0
7
This is in C function cfun...
Segmentation fault (core dumped)

有什么建议?

推荐答案

您无法收到在C FORTRAN数组作为双** ,它应该是双* ,那么试试这个

You can't recieve a fortran array in c as double **, it should be double *, so try this

#include <stdio.h>

void cfun(double *x, const int len)
{
    printf("%d\n", len);
    printf("This is in C function cfun...\n");

    for (int i = 0 ; i < len ; i++)
    {
        printf(" %d\n  %d\n  %d\n", x[i]);
    }
}

其实,如果你有交流指针,你应该加入阵列成一个阵列将它传递给FORTRAN,见例如如何使用双** 阵列< A HREF =htt​​p://www.physics.orst.edu/~rubin/nacphy/lapack/$c$cs/linear-c.html相对=nofollow> LAPACK 在C。

究其原因是,在一个Fortran二维数组连续存储,而在C 双** 是一个指针数组,因此价值没有连续存储

The reason is that in in a fortran 2d array is stored contiguously, whereas in c double ** is an array of pointers, and hence the values are not contiguously stored.

请注意在打印值的时候,你将打印错误的价值观,因为您不使用相应的格式说明双你应该修复 printf的行,使它看起来像这样

Note that when printing the values, you will print wrong values, because you are not using the appropriate format specifier for double you should fix the printf line to make it look like this

printf(" %f\n  %f\n  %f\n", x[i]);

这篇关于传递Fortran数组到C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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