问题
Consider the following code:
subroutine tao_show_this ()
implicit none
type b_struct
integer, pointer :: good_user => null()
end type
type a_struct
type (b_struct), allocatable :: value_ptr(:)
end type
type (a_struct) a
a = a_struct()
end subroutine
Compiling with gfortran 5 or 7 gives:
gfortran -c test.f90
test.f90:4:13:
type b_struct
1
Error: The rank of the element in the structure constructor at (1) does not match that of the component (0/1)
This code compiles fine with ifort. Is this a gfortran bug or is there something wrong with my code?
回答1:
For a default structure constructor, being able to omit the value for an allocatable component is a feature introduced in Fortran 2008.
gfortran does not currently support this feature ("Unimplemented features").
To leave the component not allocated while still giving a value to the constructor one references null
:
a = a_struct(NULL())
As DavidS comments, this exists as a reported bug.
来源:https://stackoverflow.com/questions/52712245/gfortran-complains-about-rank-of-element-in-structure-constructor-for-allocatabl