Arrays of pointers

前端 未结 1 1396
逝去的感伤
逝去的感伤 2020-11-29 04:18

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             


        
相关标签:
1条回答
  • 2020-11-29 04:29

    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.

    0 讨论(0)
提交回复
热议问题