无法从FORTRAN传递数组到C [英] Unable to pass array from FORTRAN to C

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

问题描述

我试图从一个FORTRAN程序传递一个一维数组到C

C函数的调用,但它持有的值是垃圾。但是,尽管如果我尝试调用使用一个整型变量同样的功能,我能够通过所需的值。谁能帮我这个?

在code我现在用的就是类似这样

文件:fortran_prog.f

 程序测试
    真实* 4 ::一个(4)
    数据/ 1,2,3,4 /
    调用test_func(一)
结束程序测试

文件:c_prog.c

  INT test_func(加倍[]){
    INT I;    对于(I = 0; I&下; 4;我++){
        的printf(%F \\ N,一个[我]);
    }    返回0;
}


解决方案

传递Fortran和C之间的阵列是一个不平凡的问题。特定的C和Fortran编译器的问题。

我看到的第一个问题是,您所指定双击来匹配真实* 4 。这当然是在几乎所有平台无效。声明C函数为:

  INT test_func(浮点*一)

这可能会在某些平台上工作,虽然很多Fortran编译器通过阵列描述符,而不是数组本身的地址。查看文档,Fortran编译器。

I am trying to pass a single dimension array from a FORTRAN program to C.

The C function is called but the values that it holds are garbage. But whereas if I try calling the same function using an integer variable I am able to pass the required value. Can anyone help me out with this?

The code I am using is similar to this

File: fortran_prog.f

program test
    real*4 :: a(4)
    data a / 1,2,3,4 /
    call test_func(a)
end program test

File: c_prog.c

int test_func(double a[]) {
    int i;

    for(i=0;i<4;i++) {
        printf("%f\n",a[i]);
    }

    return 0;
}

解决方案

Passing arrays between Fortran and C is a non-trivial problem. The particular C and Fortran compilers matter.

The first problem I see is that you specify double to match real*4. That is certainly invalid on almost all platforms. Declare the C function as:

int test_func (float *a)

This could work on some platforms, though many Fortran compilers pass the address of an "array descriptor" and not the array itself. Check the documentation for the Fortran compiler.

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

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