caimaxwell 发表于 2023-3-8 17:11:18

在使用type时如何将变量顺便设置成整型?

大神们我又来了。代码如下:type Subblocks
    integer*4:: row
    integer*4:: coL
end type Subblocks
type blocks
    integer*4:: row1,row2,coL1,coL2
    integer*4:: rowm,coLm
    integer*4:: dad
    integer*4:: son(4)
    integer*4:: bnum
    type(Subblocks), allocatable :: iSubblocks(:)
end type blocks

type(blocks), allocatable :: iblocks(:)

这段声明我是想完成这样一个操作:iblocks(i)%isubblocks(j)%coL


我想输出iblocks(i)、iblock(i)%isubblocks(j)、isubblocks(i)都是整数,想问问应该怎么声明呢?

caimaxwell 发表于 2023-3-8 17:12:15

type(Subblocks), allocatable:: iSubblocks(:)

风平老涡 发表于 2023-3-8 21:26:06

本帖最后由 风平老涡 于 2023-3-8 23:38 编辑

caimaxwell 发表于 2023-3-8 17:12


type Subblocks
    integer*4:: row
    integer*4:: coL
end type Subblocks

type blocks
    integer*4:: row1,row2,coL1,coL2
    integer*4:: rowm,coLm
    integer*4:: dad
    integer*4:: son(4)
    integer*4:: bnum
    type(Subblocks), allocatable :: iSubblocks(:)
end type blocks

type(blocks), allocatable :: iblocks(:)
type(Subblocks), allocatable:: iSubblocks(:)
integer :: i, n, m



n = 10
m = 20

allocate(iblocks(n))

do i = 1, n
   allocate(iblocks(i) % isubblocks(m))
end do

allocate(isubblocks(m))

print *, iblocks(3) % colm, iblocks % isubblocks(2) % col, isubblocks(3) % col



衍生类型的变量名(iblocks % isubblocks) 与 变量名(isubblocks)不存在冲突。

fcode 发表于 2023-3-9 08:41:19

iblocks(i)、iblock(i)%isubblocks(j)、isubblocks(i) 这些都是结构体(或叫派生类型),怎么能是整数呢?做不到的。

caimaxwell 发表于 2023-3-9 11:20:29

风平老涡 发表于 2023-3-8 21:26

type Subblocks
    integer*4:: row


太厉害了,我想了好久,才想明白问题所在,做出差不多得声明类型。但是不及您的美观、透彻。感谢!

caimaxwell 发表于 2023-3-9 11:21:41

fcode 发表于 2023-3-9 08:41
iblocks(i)、iblock(i)%isubblocks(j)、isubblocks(i) 这些都是结构体(或叫派生类型),怎么能是整数呢? ...

是的,后来我才稍微想明白,现在理解了,十分感谢您的解惑!
页: [1]
查看完整版本: 在使用type时如何将变量顺便设置成整型?