见如下注释
[Fortran] 纯文本查看 复制代码 subroutine ricker(fz,tmax,dt,prick) Bind(C,Name="ricker")
use,intrinsic :: iso_c_binding
implicit none
integer(c_int) , value :: fz,tmax !//传值
integer(c_int) :: i
real(c_float) , value :: dt!//传值
type(C_PTR) , value :: prick !//C指针
real(C_FLOAT) , pointer :: rick(:) !//fortran指针
real(C_FLOAT) :: t
real,parameter :: PAI=3.141593
call c_f_pointer( prick , rick , [tmax+1]) !//转换
rick(0:) => rick(1:)
open(10,file='ricker.dat')
do i=0,tmax
t=i*dt
!rick(i)=(1-2*(PAI*fz*(t-1.2/fz))**2)*exp(-(PAI*fz*(t-1.2/fz))**2)
rick(i)=0.0
write(10,*) t,rick(i)
end do
close(10)
end subroutine ricker
[C] 纯文本查看 复制代码 #include <stdio.h>
void ricker(int fz,int tmax,float dt,float rick[]);
int main(){
const int tmax=500;// tmax 必须为const,否则需要malloc为rick分配空间
int fz;
float dt,t,rick[tmax+1];
fz=40;
dt=0.001;
ricker(fz,tmax,dt,rick);
printf("ricker wavelet is computed!");
getchar();
return 0;
} |