[英]Allocate only one dimension for a 2D array in fortran
假設我有一個二維數組A(:,2),其中只有第一維的大小是未知的。 是否可以僅分配A的第一維? 如果不是,我每次都必須通過將A視為A(:, :)來使用“ allocate(A(n,2))”。
如果第二維始終為2,則可以創建具有兩個變量的數據類型,然后分配它們的數組:
program main
implicit none
type two_things
integer :: first
integer :: second
end type two_things
type(two_things), dimension(:), allocatable :: A
allocate(A(100))
A(1)%first = 1
A(1)%second = 2
print*, A(1)%first, A(1)%second, shape(A)
deallocate(A)
end program main
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.