指针类型转换 - 悲催的科学匠人 - 冷水's blog

指针类型转换

冷水 posted @ 2011年8月05日 19:03 in fortran with tags fortran 指针 类型转换 , 1770 阅读

采用fortran 2003标准iso_c_binding模块完成

program test
  use iso_c_binding
  implicit none
  
  interface getptr
  function fgetptr(string,ilen,ni,nj)
  use iso_c_binding
  implicit none
  real,pointer,dimension(:,:) :: fgetptr
  integer :: ilen,ni,nj
  character ::string(ilen)
  end function
  function ggetptr(string,ilen,irank,ishape)
  use iso_c_binding
  implicit none
  real,pointer,dimension(:,:) :: ggetptr
  integer :: ilen,irank
  integer :: ishape(irank)
  character ::string(ilen)
  end function

  function hgetptr(string,ilen,ishape)
  use iso_c_binding
  implicit none
  real,pointer,dimension(:,:) :: hgetptr
  integer :: ilen
  integer,dimension(:) :: ishape
  character ::string(ilen)
  end function    
  end interface   
  
  interface
    function getchar(string)
    character,pointer,dimension(:) :: getchar,string
    end function
  end interface
  
  type(c_ptr) :: p
  !character,target :: string(400)
  character,pointer,dimension(:) :: string,pp
  real,pointer,dimension(:,:) :: idata
  integer :: ishape(2)
  ishape(1) = 25
  ishape(2) = 4

  allocate(string(400))
  
  call convt1(string, 25,4,idata)
  
  write(*,*) '============================='
  idata => fgetptr(string,400,25,4)
  write(*,*) idata
  
  write(*,*) '============================='
  idata => ggetptr(string,400,2,ishape)
  write(*,*) idata
  
  write(*,*) '============================='
  idata => getptr(getchar(string),400,ishape)
  write(*,*) idata
  
end

function getchar(string)
   character,pointer,dimension(:) :: getchar,string
   
   getchar => string
   
end function


function fgetptr(string,ilen,ni,nj)
  use iso_c_binding
  implicit none
  real,pointer,dimension(:,:) :: fgetptr
  integer :: ilen,ni,nj
  character ::string(ilen)
  !
  type(c_ptr) :: p
  
  write(*,*) size(string)
  p = c_loc(string)
  CALL c_f_pointer(p,fgetptr,(/ni,nj/))
  
end function



function ggetptr(string,ilen,irank,ishape)
  use iso_c_binding
  implicit none
  real,pointer,dimension(:,:) :: ggetptr
  integer :: ilen,irank
  integer :: ishape(irank)
  character ::string(ilen)
  !
  type(c_ptr) :: p
  
  write(*,*) size(string)
  p = c_loc(string)
  CALL c_f_pointer(p,ggetptr,ishape)
  
end function



function hgetptr(string,ilen,ishape)
  use iso_c_binding
  implicit none
  real,pointer,dimension(:,:) :: hgetptr
  integer :: ilen
  integer,dimension(:) :: ishape
  character ::string(ilen)
  !
  type(c_ptr) :: p
  integer :: irank 
  irank = size(shape(hgetptr)) 
  
  write(*,*) size(string),irank
  p = c_loc(string)
  CALL c_f_pointer(p,hgetptr,ishape(1:irank))
  
end function


subroutine convt1(odata, ni,nj,tdata)
  implicit none
  integer :: ni,nj
  real,target :: odata(ni,nj)
  real,pointer,dimension(:,:) :: tdata
  
  integer :: i,j
  real :: n
  n=1.
  do j=1,nj
  do i=1,ni
    odata(i,j) = n
    n=n+1. 
  enddo
  enddo
  
  return
end subroutine
Avatar_small
Macys Credit Card Lo 说:
2022年8月19日 08:10

Macy's Credit Card. Want to pay your bill online? Sign in to access your account and make a one-time payment, Macys Credit Card Login or set up monthly auto-payments for hassle-free. Explore Entertainment Access ... If your Macy's Credit Card Account is closed by you or us, or you fail to make a qualifying purchase on your Macy's Credit Card 2021.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee