Fortran pointers to structures

前端 未结 1 616
北荒
北荒 2021-01-20 06:58

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

相关标签:
1条回答
  • 2021-01-20 07:08

    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.

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