f



access pointer to pointer from Fortran

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.
0
2/19/2010 3:01:05 PM
comp.lang.fortran 11941 articles. 2 followers. Post Follow

5 Replies
390 Views

Similar Articles

[PageSpeed] 0

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
0
burnus (564)
2/19/2010 3:21:17 PM
bio_amateur <hoangtrongminhtuan@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
0
gah (12851)
2/23/2010 2:53:59 AM
Tobias Burnus <burnus@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
0
gah (12851)
2/23/2010 3:06:12 AM
 void foofunc (int ** i, double** j)


> INTERFACE
> =A0 subroutine foofunc(i,j) bind(C)
> =A0 =A0 use iso_c_binding
> =A0 =A0 type(c_ptr) :: i,j =A0! w/o "VALUE" -> **, w/ -> *
> =A0 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
0
jimxia (61)
2/23/2010 3:16:38 AM
Jim Xia <jimxia@hotmail.com> wrote:
(snip)

>> 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.

Yes, j should be a double precision pointer.

As with C (void*) it is the programmers responsibility to keep
track of this and get it right.

-- glen
0
gah (12851)
2/23/2010 3:27:12 AM
Reply: