I am trying to implement an array of pointers, so that I can loop over the elements. However I am not sure how to do this correctly:
TYPE(domain),POINTER :: d01,d02,d03
TYPE(domain),DIMENSION(:),POINTER :: dom
...
dom(1) => d01
dom(2) => d02
dom(3) => d03
...
and then:
...
IF(ASSOCIATED(dom(2),d02))THEN
...
The compiler (pgf90 10.6-0 64-bit target on x86-64 Linux -tp istanbul-64) gives me this error message:
PGF90-S-0074-Illegal number or type of arguments to associated - keyword argument pointer (test_ptr.f90: 10)
0 inform, 0 warnings, 1 severes, 0 fatal for MAIN
As far as I understand, there is something wrong about how I subset an array of pointers. Both dom(2)
and d02
are rank 0 (scalar pointers). What is the correct way to implement this?
Thanks.
Yeah, pointer arrays are funny in Fortran.
The problem is that this:
TYPE(domain),DIMENSION(:),POINTER :: dom
does not define an array of pointers, as you might think, but a pointer to an array. There's a number of cool things you can do with these things in Fortran - pointing to slices of large arrays, even with strides - but it is definitely a pointer to an array, not an array of pointers.
The only way to get arrays of pointers in Fortran is to define a type:
type domainptr
type(domain), pointer :: p
end type domainptr
type(domainptr), dimension(3) :: dom
dom(1)%p => d01
dom(2)%p => d02
dom(3)%p => d03
etc. As far as I can tell, the only real reason you have to do this in Fortran is syntax. I'd love to see this fixed in some later version of the standard.