- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi!
I am trying to access diffdrent sections of a 1d array using the C_F_POINTER construct. For example, I have the followings code,
real(dp), allocatable :: ra(:)
complex(dp), allocatable :: ca(:)
integer :: M
integer :: N
integer :: i
integer k
allocate (ra(N), ca(M))
! initialize ra
do i = 1,N
ra(i) = dble(i)
end do
Now, I want to access various sections of RA and load them into CA using pointers. I do not want to use loops to copy data in and out of these arrays. For example, I would like to have,
k = 10 ! offset value
do i = 1,M
ca(i) = cmplx(ra(2i-1+k), ra(2i+k))
end do
If k = 0, the following code will work,
use ISO_C_BINDING
complex(dp), allocatable, target :: ca(:)
real(dp), pointer :: ra(:)
type(c_ptr) ptr_ca
allocate (ca(M))
call c_f_pointer(ptr_ca, rdata, [2*M])
MY problem is that I do not want to always associate the first 2*M values of rdata to ca. I would like to have an offset so I can associate rdata(k:k+2*M-1) to ca(1:M).
Is there a way I can do this using these pointers?
Thank you very much for your help.
Sincerely,
David
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
real(dp), allocatable :: ra(:)
complex(dp), allocatable :: ca(:)
integer :: M
integer :: N
integer :: i
integer k
allocate (ra(N), ca(M))
! initialize ra
do i = 1,N
ra(i) = dble(i)
end do
ca(1:N/2) = transfer(ra(1:N), (0.d0,0.d0), N/2)
That is all I needed!

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