일부 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
메모리이야.
그래서 제 질문은 string
변수를 에 할당하려면 어떻게해야 getValueFromC
합니까?
한 가지 해결책은 반복해서 문자열 조각에 할당하는 것입니다. 나는 이것이 100 % 옳다는 것을 확인하지 않았지만 나를 위해 컴파일됩니다 ...
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 /) )
do i = 1, min(200, resultLength)
getValueFromC(i:i) = string(i)
end do
call c_bridge_releaseString(resultString)
end function getValueFromC
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다