Passing Fortran arrays to C

雨燕双飞 提交于 2019-12-08 18:43:30

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]);
    }
}

in fact if you have a c double ** array of pointers you should join the arrays into a single array to pass it to fortran, see for example how to use Lapack in 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.

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]);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!