Fortran 如何使用维度属性?
我需要找到如何在此程序中使用Fortran 如何使用维度属性?,fortran,fortran90,Fortran,Fortran90,我需要找到如何在此程序中使用维度属性。这里我无法解决的问题是,用户如何指定行数?(换言之,学生人数): 基本上,您有固定(静态)数组,这些数组是使用维度定义的: real,dimension(4) :: X X是长度为4(1-4)的数组。这相当于: real :: X(4) 静态数组在其作用域中具有固定长度(例如,在全局变量的整个程序中或在函数/子例程中) 您需要的是allocate数组,这些数组在运行时是allocated: program test implicit none r
维度
属性。这里我无法解决的问题是,用户如何指定行数?(换言之,学生人数):
基本上,您有固定(静态)数组,这些数组是使用
维度定义的:
real,dimension(4) :: X
X
是长度为4
(1
-4
)的数组。这相当于:
real :: X(4)
静态数组在其作用域中具有固定长度(例如,在全局变量的整个程序中或在函数/子例程中)
您需要的是allocate
数组,这些数组在运行时是allocate
d:
program test
implicit none
real, allocatable :: B(:) ! The shape is given by ":" - 1 dimension
integer :: stat
! allocate memory, four elements:
allocate( B(4), stat=stat )
! *Always* check the return value
if ( stat /= 0 ) stop 'Cannot allocate memory'
! ... Do stuff
! Clean up
deallocate( B )
! Allocate again using a different length:
allocate( B(3), stat=stat )
! *Always* check the return value
if ( stat /= 0 ) stop 'Cannot allocate memory'
! No need to deallocate at the end of the program!
end program
这管用!谢谢你们。你们什么意思?这有用?您显示的代码片段肯定不起作用。
program test
implicit none
real, allocatable :: B(:) ! The shape is given by ":" - 1 dimension
integer :: stat
! allocate memory, four elements:
allocate( B(4), stat=stat )
! *Always* check the return value
if ( stat /= 0 ) stop 'Cannot allocate memory'
! ... Do stuff
! Clean up
deallocate( B )
! Allocate again using a different length:
allocate( B(3), stat=stat )
! *Always* check the return value
if ( stat /= 0 ) stop 'Cannot allocate memory'
! No need to deallocate at the end of the program!
end program
real,dimension(:,:),allocatable ::A
character(len=10),dimension(:),allocatable::B
.
.
.
DEALLOCATE(A)
DEALLOCATE(B)