我试图将一些C代码桥接到Fortran中。但是,我无法将C API返回的可变长度C字符串转换为Fortran API所需的固定长度字符串。将可变长度字符串分配给固定长度字符串
这是一个缩减版本的代码,不会编译 - 我得到The shapes of the array expressions do not conform
。
character*200 function getValueFromC()
use cbridge
implicit none
type(c_ptr) :: resultString
integer(kind=c_int) :: resultLength
character, pointer, dimension(:) :: string
call c_bridge_getValue(bridge, resultString, resultLength)
call c_f_pointer(resultString, string, (/ resultLength /))
getValueFromC = string
call c_bridge_releaseString(resultString)
end function getValueFromC
cbridge
只是含有c_bridge_getValue()
和c_bridge_releaseString
定义模块和bridge
指针(只是一个void*
)
c_bridge_getValue()
只是malloc
s上行一个新的字符串并返回它,c_bridge_releaseString()
free
S上的存储器。
所以我的问题是,我需要做些什么来将string
变量分配给getValueFromC
?
总是使用标签fortran的Fortran问题。而你的代码是Fortran2003,所以fortran90标签是不合适的。 –