问题描述
在Fortran 90及更高版本中可以分配数组.
Allocatable arrays are possible in Fortran 90 and up.
INTEGER, ALLOCATABLE, DIMENSION(:) :: test_int_array
在Fortran 2003中,可分配标量(例如可分配字符)是可能的.
Allocatable scalars such as allocatable characters are possible in Fortran 2003.
CHARACTER(LEN=:), ALLOCATABLE :: test_str
我想知道是否可以声明可分配字符的数组(固定或可分配)? (可能就像下面的内容一样,不幸的是不会编译.)
I am wondering is it possible to declare an array, fixed or allocatable, of allocatable characters? (Possibly like something below, which does not compile unfortunately.)
CHARACTER(LEN=:), ALLOCATABLE, DIMENSION(4) :: test_str_array
推荐答案
program test_alloc
character (len=:), allocatable :: string
character(len=:), allocatable :: string_array(:)
type my_type
character (len=:), allocatable :: my_string
end type my_type
type (my_type), dimension (:), allocatable :: my_type_array
string = "123"
write (*, *) string, len (string)
string = "abcd"
write (*, *) string, len (string)
allocate(character(5) :: string_array(2))
string_array (1) = "1234"
string_array (2) = "abcde"
write (*, *) string_array (1), len (string_array (1))
write (*, *) string_array (2), len (string_array (2))
allocate (my_type_array (2))
my_type_array (1) % my_string = "XYZ"
my_type_array (2) % my_string = "QWER"
write (*, *) my_type_array (1) % my_string, len (my_type_array (1) % my_string)
write (*, *) my_type_array (2) % my_string, len (my_type_array (2) % my_string)
end program test_alloc
我在 http://software.intel找到了语法.com/en-us/forums/showthread.php?t = 77823 .它适用于ifort 12.1,但不适用于gfortran 4.6.1.尝试创建用户定义类型的尝试也不起作用.
I found the syntax at http://software.intel.com/en-us/forums/showthread.php?t=77823. It works with ifort 12.1 but not with gfortran 4.6.1. Trying the work around of creating a user-defined type didn't work either.
这篇关于如何在Fortran中声明可分配标量数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!