|
问题:
从C传递数组到Fortran,分别通过interface定义了subroutine和function的形式,前者未起作用,而后者却可以
编译器:gcc/gfortran 4.7&4.8
fortran代码:
[Fortran] 纯文本查看 复制代码 02 | use , intrinsic :: iso_c_binding |
06 | subroutine dynamic_array ( length , a , n ) bind ( c , name = 'dynamic_array' ) |
09 | integer ( c_int ) , intent ( in ) , value :: length |
10 | integer ( c_int ) , intent ( out ) :: n |
11 | type ( c_ptr ) , intent ( out ) :: a |
14 | type ( c_ptr ) function dynamic_array 2 ( length , n ) bind ( c , name = 'd_array' ) |
17 | integer ( c_int ) , intent ( in ) , value :: length |
18 | integer ( c_int ) , intent ( out ) :: n |
27 | real ( c_float ) , allocatable :: array 2 ( : ) |
37 | call dynamic_array ( i , c_array , j ) |
39 | call c_f_pointer ( c_array , array , [ j ] ) |
40 | write ( * , '(A)' ) "Pass by subroutine" |
41 | write ( * , '(A,i4)' ) "Len of array:" , j |
42 | write ( * , '(A)' ) "Array values:" |
51 | c_array = dynamic_array 2 ( i , j ) |
52 | call c_f_pointer ( c_array , array , [ j ] ) |
53 | write ( * , '(A)' ) "Pass by function" |
54 | write ( * , '(A,i4)' ) "Len of array:" , j |
55 | write ( * , '(A)' ) "Array values:" |
C代码:[C] 纯文本查看 复制代码 04 | void print_float_array( float *array, int len) |
08 | printf ( "%f | " , array[i]); |
12 | void dynamic_array( int n1, float *a, int *n2) |
16 | a = ( float *) malloc (n1*2* sizeof ( float )); |
18 | for (i=0;i<n1*2;i++){a[i] = ( float )i*i+1.0;} |
24 | float * d_array( int n1, int *n2) |
29 | a = ( float *) malloc (n1*2* sizeof ( float )); |
31 | for (i=0;i<n1*2;i++){a[i] = ( float )i*i+1.0;} |
|
|