Fortran指向结构的指针 [英] Fortran pointers to structures

查看:636
本文介绍了Fortran指向结构的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,指向结构的指针,指向结构的指针。
我使用gfortran 4.6.3,文件名为test_pointer_struct.f08,所以我使用Fortran 2008标准(由gfortran 4.6.3支持)。
$ b $ Hera的代码如下:

  PROGRAM test_pointer_struct 

type tSmall
integer :: a
double precision :: b
end type tSmall

type tBig
integer :: h
type(tSmall),pointer :: member_small
结束类型tBig

类型(tBig):: var_big
类型(tSmall),pointer :: var_small(:)

!我们得到一个指向小结构的数组
allocate(var_small(3))
!还分配了member_small结构(不是数组)
分配(var_big%member_small)

var_big%member_small%a = 1
var_big%member_small%b = 2.0

!现在我们需要var_samall指针数组的一个元素来指向member_big中的member_small
var_small(1)=> var_big%member_small! < - 我在这里得到一个编译错误

!并解散member_small(我们仍然通过var_small(1)
var_big%member_small => NULL()

保持对内存空间的访问END程序test_pointer_struct

当我编译时,出现以下错误:
错误:Se' )
其中可能被翻译为
错误:在'(1)处限制'var_small'的规格说明



这个错误是什么意思?我是否做错了?



非常感谢您提前。

解决方案

<你的声明

  type(tSmall),pointer :: var_small(:) 

没有将 var_small 定义为指向 tsmall 类型的东西的指针数组;而是将它定义为一个指向类型的东西的数组的指针。tsmall



当我编译代码时,英特尔Fortran提供了相当有用的错误消息:
$ b


此数据指针赋值语法不正确:'bound
spec'或'bound remapping'是预期在这种情况下。

这将我们带入Fortran 2003标准中的R735 。编译器试图将 var_small(1)不是作为对指针数组中第一个元素的引用,而是对 bounds-spec -list 边界重新映射列表。表达式没有正确的语法,解析失败。



所以这涉及到错误意味着什么的问题。你对此做了什么?这取决于你的意图。通常的建议是定义一个派生类型,沿着这些线

 类型myptr 
类型(tsmall),指针: :psmall
结束类型myptr

然后使用这些数组

  type(myptr),dimension(:),allocatable :: ptrarray 

就我个人而言,我从来不喜欢这种方法,并且从来不需要使用它(我编写的程序非常简单)。我预计在Fortran 2003中也有更好的方法,但不知道你的意图,我不愿意提供建议。


I have a problem assigning a pointer to a structure, to a pointer to a structure. I use gfortran 4.6.3, and the name of the file is test_pointer_struct.f08 so I am using the Fortran 2008 standard (as supported by gfortran 4.6.3).

Hera comes the code:

PROGRAM test_pointer_struct

type tSmall
  integer          :: a
  double precision :: b
end type tSmall

type tBig
  integer                   :: h
  type(tSmall), pointer     :: member_small
end type tBig

type(tBig)                  :: var_big
type(tSmall), pointer       :: var_small(:)

! We get an array of pointers to the small structure
allocate(var_small(3))
! Also allocate the member_small strucutre (not an array)
allocate(var_big%member_small)

var_big%member_small%a = 1
var_big%member_small%b = 2.0

! Now we want an element of the var_samall array of pointers, to point to the member_small in member_big
var_small(1) => var_big%member_small    ! <- I get a compilation error here

! And dissasociate the member_small (we still maintain access to memory space through var_small(1)
var_big%member_small => NULL()

END PROGRAM test_pointer_struct

When I complie this, I get the following error: Error: Se esperaba una especificación de límites para 'var_small' en (1) Which could be translated as Error: Limit specification expected for 'var_small' at (1)

What does this error mean?. What am I doing wrong?

Thank you very much in advance.

解决方案

Fortran doesn't really do arrays of pointers. Your declaration

type(tSmall), pointer       :: var_small(:)

doesn't define var_small to be an array of pointers to things of type tsmall; rather it defines it to be a pointer to an array of things of type tsmall.

When I compile your code Intel Fortran gives the rather more helpful error message

The syntax of this data pointer assignment is incorrect: either 'bound spec' or 'bound remapping' is expected in this context.

which takes us to R735 in the Fortran 2003 standard. The compiler tries to parse var_small(1) not, as you wish, as a reference to the first element in an array of pointers but to either a bounds-spec-list or a bounds-remapping-list. The expression does not have the right syntax for either and the parse fails.

So that deals with the question of what the error means. What do you do about it ? That depends on your intentions. The usual suggestion is to define a derived type, along these lines

type myptr
    type(tsmall), pointer :: psmall
end type myptr

and then use an array of those

type(myptr), dimension(:), allocatable :: ptrarray

Personally I've never liked that approach and have never needed to use it (I write very simple programs). I expect that with Fortran 2003 there are better approaches too but without knowing your intentions I hesitate to offer advice.

这篇关于Fortran指向结构的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆