问题描述
我正在尝试实现一个指针数组,以便我可以遍历元素.但是我不确定如何正确执行此操作:
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
...
然后:
...
IF(ASSOCIATED(dom(2),d02))THEN
...
编译器(x86-64 Linux -tp istanbul-64 上的 pgf90 10.6-0 64 位目标)给了我这个错误消息:
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
据我所知,我如何对指针数组进行子集化存在一些问题.dom(2)
和 d02
都是 0 级(标量指针).实现此目的的正确方法是什么?
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?
谢谢.
推荐答案
是的,指针数组在 Fortran 中很有趣.
Yeah, pointer arrays are funny in Fortran.
问题在于:
TYPE(domain),DIMENSION(:),POINTER :: dom
并没有像你想象的那样定义一个指针数组,而是一个指向数组的指针.在 Fortran 中,你可以用这些东西做很多很酷的事情——指向大数组的切片,即使有 strides——但它绝对是一个指向数组的指针,而不是一个指针数组.
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.
在 Fortran 中获取指针数组的唯一方法是定义一个类型:
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
等等.据我所知,您必须在 Fortran 中执行此操作的唯一真正原因是语法.我很想在标准的更高版本中看到这个问题.
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.
这篇关于指针数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!