Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29277 Discussions

Having trouble accessing different parts of an array using C_F_POINTER?

david_sallngc_com
424 Views

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

0 Kudos
1 Reply
david_sallngc_com
424 Views
I have made this much more difficult than I thought. I figured it out using the TRANSFER intrinsic. This does exactly what I need.

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!
0 Kudos
Reply