int get_table(char ***Results );
interface
function get_table(Results) bind(c)
use iso_c_binding, only: c_int, c_char
character(kind=c_char),dimension(*)::Results
integer(c_int):: get_table
end function
end interface
character(8) :: Results(3,10)
i = get_table(Results)
use ISO_C_Binding
implicit none
interface
integer(c_int) function get_table(val) bind(c)
import
type(c_ptr) :: val
end function
end interface
character(8) ,target :: Results(3,10)
type(c_ptr) , target :: val
integer :: i
val = c_loc(Results)
i = get_table(c_loc(val))
write(*,*) Results(1,1)
end
#include <stdio.h>
int get_table(char ***Results){
(**Results)[0] = 'f';
(**Results)[1] = 'c';
(**Results)[2] = 'o';
(**Results)[3] = 'd';
(**Results)[4] = 'e';
}
use ISO_C_Binding
implicit none
interface
integer(c_int) function get_table(val) bind(c)
import
type(c_ptr) :: val
end function
end interface
character(8) ,target :: Results(3,10)
type(c_ptr) , target :: val
integer :: i
val = c_loc(Results)
i = get_table(c_loc(val))
write(*,*) Results(1,1)
end
#include <stdio.h>
int get_table(char ***Results){
Results[0][0][0] = 'f';
Results[0][0][1] = 'c';
Results[0][0][2] = 'o';
Results[0][0][3] = 'd';
Results[0][0][4] = 'e';
Results[3][5][0] = 'f';
Results[3][5][1] = 'c';
Results[3][5][2] = 'o';
Results[3][5][3] = 'd';
Results[3][5][4] = 'e';
return 1;
}
Program Main
use ISO_C_Binding
use CLangMultiDimensionArrayLink2D
implicit none
interface
integer(c_int) function get_table(val) bind(c)
import
type(c_ptr) , value :: val
end function
end interface
Integer , parameter :: M = 6 , N = 4
character(8) ,target :: Results(M,N)
integer :: i
type(CLang2DArrayLink) :: linkResults
i = get_table( linkResults%Set(Results) )
write(*,"(2(a8,1x))") Results(1,1),Results(M,N)
End Program Main
Module CLangMultiDimensionArrayLink2D
use , Intrinsic :: ISO_C_Binding
Implicit None
private
Type , public :: CLang2DArrayLink
type(c_ptr) , pointer :: p1(:) , p2(:,:)
contains
Procedure :: Set
Final :: Release
End Type CLang2DArrayLink
contains
Type(c_ptr) Function Set( this , datas ) result ( cptr )
class(CLang2DArrayLink) :: this
class(*) , intent(IN) :: datas(1:,1:)
integer :: sizes(2) , i , j
sizes = shape(datas)
if( associated(this%p1) ) deallocate(this%p1,this%p2)
Allocate(this%p1(sizes(2)),this%p2(sizes(1),sizes(2)))
Do i = 1 , sizes(2)
Do j = 1 , sizes(1)
this%p2(j,i) = c_loc(datas(j,i))
End Do
this%p1(i) = c_loc(this%p2(1,i))
End Do
cptr = c_loc(this%p1(1))
End Function Set
Subroutine Release( this )
type(CLang2DArrayLink) :: this
if( associated(this%p1) ) deallocate(this%p1,this%p2)
End Subroutine Release
End Module CLangMultiDimensionArrayLink2D
fcode 发表于 2021-7-31 09:05
C函数应该是一个三维数组,你可能需要构建一个指针链给他用。
[mw_shl_code=c,true]#include
Project.jpg (52.71 KB, 下载次数: 439)
1.73 MB, 下载次数: 27
type(c_ptr),target :: val
type(c_ptr),pointer:: p1(:)
istat = sqlite3_get_table(db,"select * from MY_TABLE",val,nRow,nColumn,ErrMsg)
call c_f_pointer(val,p1,[nRow*nColumn])
Do i = 1 , nRow
Do j = 1 , nColumn
call c_f_pointer( p1( (i-1)*nColumn + j ),cRes)
k = index(cRes,c_null_char)
Results(j,i) = cRes(:k-1)
End Do
End Do
布衣龙共 发表于 2021-8-16 16:14
原来是 sqlite。sqlite3_get_table 的参数,你给他一个指针,由它内部分配空间,最后把它分配的空间的地址 ...
character(kind=c_char,len=256),pointer :: cRes
布衣龙共 发表于 2021-8-16 17:44
可以,没关系的。
Fortran 的指针不需要释放,C那边你找找,可能有别的函数来释放。 ...
布衣龙共 发表于 2021-8-16 16:14
原来是 sqlite。sqlite3_get_table 的参数,你给他一个指针,由它内部分配空间,最后把它分配的空间的地址 ...
istat = sqlite3_open(trim(filename)//""C, db)
sql = "CREATE TABLE MY_TABLE (ID INTEGER PRIMARY KEY AUTOINCREMENT, "// &
"DATA1 TEXT NOT NULL, " // &
"DATA2 TEXT )"
istat = sqlite3_exec(db, sql, c_null_funptr, c_null_ptr, ErrMsg)
istat = sqlite3_exec(db,"begin;",c_null_funptr, c_null_ptr, ErrMsg)
sql = "INSERT INTO MY_TABLE (DATA1,DATA2) VALUES (?,?);"C
istat = sqlite3_prepare_v2(db,sql,len_trim(sql),stmt,c_null_ptr)
do i=1,50
istat = sqlite3_bind_text(stmt, 1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 32, c_null_ptr);
istat = sqlite3_bind_text(stmt, 2, "0123456789", 16, c_null_ptr);
istat = sqlite3_step(stmt)
enddo
istat = sqlite3_finalize(stmt)
istat = sqlite3_exec(db,"commit;",c_null_funptr, c_null_ptr, ErrMsg)
istat = sqlite3_close(db)
istat = sqlite3_bind_text(stmt, 1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 32, c_null_ptr);
sss = “ABCDEFGHIJKLMNOPQRSTUVWXYZ” ! character(256) :: sss
istat = sqlite3_bind_text(stmt, 1, trim(sss)//""C 32, c_null_ptr);
fcode 发表于 2021-8-18 11:06
sss = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"c
istat = sqlite3_bind_text(stmt, 1, sss, 32, c_null_ptr);
20210819105733.png (55.71 KB, 下载次数: 384)
fcode 发表于 2021-8-19 13:07
sqlite3_bind_text 的时候,并没有写入,只是存入地址。
等到 sqlite3_step 的时候,才会真正写入。
因为你 ...
20210820100251.png (68.21 KB, 下载次数: 358)
805 Bytes, 下载次数: 3
fcode 发表于 2021-8-20 19:29
function sqlite3_reset(stmt) bind(c)
import
type(c_ptr), value :: stmt
欢迎光临 Fortran Coder (http://bbs.fcode.cn/) | Powered by Discuz! X3.2 |