我永远不会DATA
在现代 Fortran 中使用该语句。尝试
...
INTEGER, parameter :: maxExampleTypes = 5
TYPE ExampleInfo
CHARACTER (len=50) :: ExDeckName
INTEGER :: A(maxExampleTypes)
INTEGER :: ExUnits
ENDTYPE ExampleInfo
TYPE(ExampleInfo) :: Example(10)
Example(1)%ExDeckName = 'test.dck'
Example(1)%A = (/ 1,2,3,4,5 /)
...
如果这些值应该是默认值,请将它们放入类型声明中:
...
INTEGER, parameter :: maxExampleTypes = 5
TYPE ExampleInfo
CHARACTER (len=50) :: ExDeckName = 'test.dck'
INTEGER :: A(maxExampleTypes) = (/ 1,2,3,4,5 /)
INTEGER :: ExUnits
ENDTYPE ExampleInfo
TYPE(ExampleInfo) :: Example(10)
...
示例测试程序:
module testmod
implicit none
INTEGER, parameter :: maxExampleTypes = 5
! Type with default values
TYPE ExampleInfo
CHARACTER (len=50) :: ExDeckName = 'test.dck'
INTEGER :: A(maxExampleTypes)= (/ 1,2,3,4,5 /)
INTEGER :: ExUnits
ENDTYPE ExampleInfo
contains
subroutine init_ExampleInfo(array)
implicit none
type(ExampleInfo), intent(out):: array(:)
integer :: i
do i=1,size(array)
array(i)%ExDeckName = 'test.dck'
array(i)%A = (/ 1,2,3,4,5 /)
enddo
end subroutine
end module
program test
use testmod
implicit none
TYPE(ExampleInfo) :: Example(10)
! Initialize manually
! call init_ExampleInfo(Example)
write(*,*) Example(1)%ExDeckName, Example(1)%A
! Set new values
Example(1)%ExDeckName = 'test2.dck'
Example(1)%A = (/ 5,4,3,2,1 /)
write(*,*) Example(1)%ExDeckName, Example(1)%A
end program