weixing1531 发表于 2022-7-1 10:38:07

递延长度字符串数组如何表示?

本帖最后由 weixing1531 于 2022-7-1 23:52 编辑

目前递延长度字符串功能已经十分接近VBA中可变字符string类型了,但递延长度字符串数组如何表示呢?
我目前想到的办法就是派生类型封装一下

type::string
character(len=:),allocatable::str
end type

type(string)::a(4)
a(1)%str="hi"

很想直接
character(len=:),allocatable::str(4)
但编译器会报错
请问有没有更好的办法?




楚香饭 发表于 2022-7-2 09:24:38

intel fortran 的论坛上有人提过这问题。steven大神说只能用派生类型。
https://community.intel.com/t5/I ... riables/td-p/787190
我感觉还好,起码这个派生类型是个通用的。

li913 发表于 2022-7-11 10:15:19

1、不可能直接用,那样的话相当于c的二级指针,内存不连续,fortran做不到。
2、kind和size都是可分配,这样是可以的,但需要二者同时分配,也就是不能实现递延,只能固定长度。
program Test
implicit none
character(:),allocatable:: str(:)
allocate(str,source=['abc','12345'])

print*,len(str(1)), str(1)
print*,len(str(2)), str(2)

end program

页: [1]
查看完整版本: 递延长度字符串数组如何表示?