我对 fortran 很陌生。而且我使用的是 fortran 内置的库,该库有许多 TYPE 数组。我尝试使用以下方法通过 ac 程序将值分配给 lib 中的 TYPE 数组。我已经构建了一个 c-fortran 接口,我从 sqlite 数据库获取值到 c prg 中的 ac 结构数组中。然后将此结构数组传递给一个 fortran 子例程,在其中我将其声明为派生类型,匹配声明的 TYPE 变量的定义在 lib 中。然后我将传递的数组中的值复制到 lib 中声明的实际 TYPE 数组,并将其传递给 fortran 函数。
发生的事情是数组中的值可以从 c 传递到 fortran 子例程,我打印它们以在 fortran 子例程中检查它们,但是当数组从子例程传递到函数时,这些值会变得乱码。我将数组作为假定的形状数组传递。该函数在模块内声明,因此我认为调用子例程不需要接口。
我不完全理解发生了什么,我也尝试使用 TYPE 声明中的序列。我正在使用 g95 , gcc 4.0.3 编译器。数组中的所有值都是 REAL(KIND =8) 类型,c 程序中的等价物是 double 。
考虑一个在其中声明了 TYPE(something), TYPE(Something2) 的库。我将 lib 作为模块导入到 fortran 子例程中。
让我们假设
TYPE(something_lib) is
REAL(kind =8) ::A
REAL(kind=8) ::B
END TYPE
在库中
TYPE(SOMETHING2_lib) !this is also declare in the lib
!I have a C program in which
! in which
///////////////////////////////////////// //////////////////////////////////
// C program
struct SomethingC
{
double a
double b
} ;
struct SomethingC x[2]
struct something2C s[2] // something similar to the first struct
//i fill the values in x ,s from database in proper format.(doubles).
//i call the fortran subroutine in the c program
A_(x,s); //call to fortran subroutine
///////////////////////////////////////// ////////////////////////////////////fortan 子程序
SUBROUTINE A (x,s)
USE Lib_module ! this LIB_Module also contains the function func
TYPE G
REAL(kind =8) ! this is defined similar to TYPE something(in lib) by me
REAL(kind =8)
END TYPE G
TYPE G2
similar to TYPE Something2 in lib
END TYPE G2
TYPE(something_lib) :: D(2) !derived type declared in lib
TYPE(Something2_lib)::E(2) ! derived type declared in lib
TYPE(G)::x(2)
TYPE(G2)::s(2)
! x, s are struct arrays from c which are now declared in the fortran function
copy code for
copying values from
x to D
s to E
print all values of
D
Print all values of
E
!this prints the values correct as expected for both x,d
func(D,E) ! this function is defined in the lib . The function is in the
! LIB_module
! so no interface will be required (i think)
! IN the Function
FUNCTION func(D,E) (while debugging)
TYPE(something_lib) :: INTENT (IN) D(:)
TYPE (something2_lib)::INTENT (IN) E(:)
when i try to print values of D , E in the
function i get garbled values like
1180333333
2.33419537006E-313
!when out of the function and back in the subroutine i.e after the call(while debugging)
! if I print the values of D,E here they print ok
END SUBROUTINE
因此,它们在函数中传递后会出现乱码,但在子例程中是可以的。我的问题是为什么会这样?我该如何解决?