gpt4 book ai didi

arrays - 如何在 Fortran 中声明可分配标量数组?

转载 作者:行者123 更新时间:2023-12-02 14:30:46 24 4
gpt4 key购买 nike

Fortran 90 及更高版本中可以使用可分配数组。

INTEGER, ALLOCATABLE, DIMENSION(:) :: test_int_array

可分配标量(例如可分配字符)在 Fortran 2003 中是可能的。

CHARACTER(LEN=:), ALLOCATABLE :: test_str

我想知道是否可以声明一个由可分配字符组成的固定或可分配数组? (可能像下面的内容,不幸的是它无法编译。)

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。尝试创建用户定义类型的解决方法也不起作用。

关于arrays - 如何在 Fortran 中声明可分配标量数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8403557/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com