From: bio_amateur on 19 Feb 2010 10:01 In C, we can define a variable whose content is the memory address of another one. My first question is can we do this in Fortran. Suppose that I have a C function that return a pointer to pointer void foofunc (int ** i, double** j) and I want to this function to be interoperable from Fortran. Could someone give me a solution. Thank you so much Tuan.
From: Tobias Burnus on 19 Feb 2010 10:21 On 02/19/2010 04:01 PM, bio_amateur wrote: > In C, we can define a variable whose content is the memory address of > another one. My first question is can we do this in Fortran. > > Suppose that I have a C function that return a pointer to pointer > > void foofunc (int ** i, double** j) > > and I want to this function to be interoperable from Fortran. Could > someone give me a solution. Thank you so much use iso_c_binding INTERFACE subroutine foofunc(i,j) bind(C) use iso_c_binding type(c_ptr) :: i,j ! w/o "VALUE" -> **, w/ -> * end subroutine foofunc END INTERFACE type(c_ptr) :: iptr, jptr integer, pointer :: i, j call foofunc(iptr,jptr) call c_f_pointer(iptr,i) call c_f_pointer(jptr,j) write(*,*) i, j end Tobias
From: glen herrmannsfeldt on 22 Feb 2010 21:53 bio_amateur <hoangtrongminhtuan(a)gmail.com> wrote: > In C, we can define a variable whose content is the memory address of > another one. My first question is can we do this in Fortran. > Suppose that I have a C function that return a pointer to pointer > void foofunc (int ** i, double** j) First, C Tobias' answer. Then note that your function is not returning a pointer to a pointer, but has such as arguments. With call by value in C, you have to pass a pointer to allow the data to be modified. > and I want to this function to be interoperable from Fortran. Could > someone give me a solution. Thank you so much -- glen
From: glen herrmannsfeldt on 22 Feb 2010 22:06 Tobias Burnus <burnus(a)net-b.de> wrote: > On 02/19/2010 04:01 PM, bio_amateur wrote: >> In C, we can define a variable whose content is the memory address of >> another one. My first question is can we do this in Fortran. >> Suppose that I have a C function that return a pointer to pointer >> void foofunc (int ** i, double** j) >> and I want to this function to be interoperable from Fortran. Could >> someone give me a solution. Thank you so much > use iso_c_binding > INTERFACE > subroutine foofunc(i,j) bind(C) > use iso_c_binding > type(c_ptr) :: i,j ! w/o "VALUE" -> **, w/ -> * > end subroutine foofunc > END INTERFACE > type(c_ptr) :: iptr, jptr > integer, pointer :: i, j > call foofunc(iptr,jptr) > call c_f_pointer(iptr,i) > call c_f_pointer(jptr,j) > write(*,*) i, j > end A common reason for using ** in C is to allow arrays of pointers to arrays, convenient for dynamically allocating two dimensional data structures. Fortran doesn't directly allow pointers to pointers, but does allow pointers to structures containing pointers. In this case, you will need arrays of type(c_ptr), with each pointing to the appropriate array, converted to a Fortran pointer with c_f_pointer. I think something like: use iso_c_binding INTERFACE subroutine foofunc(i,j) bind(C) use iso_c_binding type(c_ptr) :: i,j ! w/o "VALUE" -> **, w/ -> * end subroutine foofunc END INTERFACE type(c_ptr) :: iptr(10), jptr(10) integer, pointer :: i(:) double precision, pointer :: j(:) call foofunc(iptr,jptr) call c_f_pointer(iptr(3),i,(/10/)) call c_f_pointer(jptr(5),j,(/10/)) write(*,*) i, j end -- glen
From: Jim Xia on 22 Feb 2010 22:16
void foofunc (int ** i, double** j) > INTERFACE > subroutine foofunc(i,j) bind(C) > use iso_c_binding > type(c_ptr) :: i,j ! w/o "VALUE" -> **, w/ -> * > end subroutine foofunc > END INTERFACE > type(c_ptr) :: iptr, jptr > integer, pointer :: i, j > call foofunc(iptr,jptr) > call c_f_pointer(iptr,i) > call c_f_pointer(jptr,j) j is of type double in C side, and the Fortran side provides an integer. One big disadvantage of this is Fortran side loses the type checking for i or j. And don't do this often, otherwise it's begging for troubles. Type(C_PTR) is not well designed. Cheers, Jim |