Arrays of pointers

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.

Leave a Comment