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

one dimensional pointer to a two dimensional array

JohnR
Beginner
392 Views
Hi, I'd like to set up a 1D pointer to access a 2D allocatable array. EQUIVALENCE would be the perfect solution, but this isn't allowed for allocatables. The best solution I can come up with is clunky (see below). Does anyone have a better (standard) method?

real , allocatable :: Arr(:,:)
real :: Vec(*)
pointer (Addr , Vec)

integer i , j

allocate( Arr(-10:20,-13:77) )
Addr = LOC(Arr)


Thanks
John
0 Kudos
2 Replies
Steven_L_Intel1
Employee
392 Views

The following is bog-standard F2003 and works in Intel Fortran 10. Note the required TARGET attribute on Arr.

use iso_c_binding
implicit none

real, allocatable, target :: Arr(:,:)
real, pointer :: vec(:)

allocate (Arr(2,3))
Arr = reshape([1,2,3,4,5,6],[2,3])
call c_f_pointer (c_loc(Arr), vec, [size(Arr)])

print *, shape(vec)
print *, vec

end
0 Kudos
JohnR
Beginner
392 Views
Thanks Steve! This is exactly what I needed.

Regards
John
0 Kudos
Reply