- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
I have a question about the usages of function loc or c_loc.
I want to access the array a in a subroutine.
I konw that the loc or c_loc can gets the storage address of the variable.
But how to use its address to access its data .( not using 'use datamod')
In the C language , I can manage it. Can we do it in Fortran.
Thanks in advance!
!-----------------------------------------
module datamod
!-----------------------------------------
implicit none
!
real,allocatable,save :: a(:)
real,allocatable,save :: b(:,:)
!
end module datamod
!-----------------------------------------
program main
!-----------------------------------------
use datamod
implicit none
integer :: i
integer :: loc_a,loc_b
!
!allocate a ----------------------------------------
!
allocate(a(10))
do i=1,10
a(i) = real(i)
end do
!
! get the address of a
loc_a = loc(a)
write(*,*) 'the address of a storage',loc_a
!
! how can I get the data of array 'a' by its address
!
end program
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The standard way to do this:
[fortran]
program main
!-----------------------------------------
use datamod
use, intrinsic :: iso_c_binding
implicit none
integer :: i
type(c_ptr) :: loc_a
real, pointer :: ap(:)
!
!allocate a ----------------------------------------
!
allocate(a(10))
do i=1,10
a(i) = real(i)
end do
!
! get the address of a
loc_a = c_loc(a)
write(*,*) 'the address of a storage',loc_a
!
! how can I get the data of array 'a' by its address
!
call c_f_pointer(loc_a, ap, [10])
print *, ap
end program
[/fortran]
In this case I have used c_loc. If you want to use an integer to hold the address, you must declare it as integer(c_intptr_t) where c_intptr_t comes from module iso_c_binding. You can also use integer(int_ptr_kind()). If you use an integer pointer, replace loc_a in the call to c_f_pointer with transfer(loc_a, c_null_ptr).
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The standard way to do this:
[fortran]
program main
!-----------------------------------------
use datamod
use, intrinsic :: iso_c_binding
implicit none
integer :: i
type(c_ptr) :: loc_a
real, pointer :: ap(:)
!
!allocate a ----------------------------------------
!
allocate(a(10))
do i=1,10
a(i) = real(i)
end do
!
! get the address of a
loc_a = c_loc(a)
write(*,*) 'the address of a storage',loc_a
!
! how can I get the data of array 'a' by its address
!
call c_f_pointer(loc_a, ap, [10])
print *, ap
end program
[/fortran]
In this case I have used c_loc. If you want to use an integer to hold the address, you must declare it as integer(c_intptr_t) where c_intptr_t comes from module iso_c_binding. You can also use integer(int_ptr_kind()). If you use an integer pointer, replace loc_a in the call to c_f_pointer with transfer(loc_a, c_null_ptr).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks for your answer.

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page