在Fortran 90中,数组维必须事先声明吗? [英] In Fortran 90, do array dimensions have to be declared beforehand?

查看:164
本文介绍了在Fortran 90中,数组维必须事先声明吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有必要在任何其他代码之前声明数组维度?例如,我写了以下简化示例代码:

Is it necessary to declare array dimensions before any other code? For example, I have written the following simplified example code:

PROGRAM mytest
  IMPLICIT NONE
  INTEGER :: i, j, k, mysum

  ! Let array c be a k-by-k**2 array
  ! Determine k within the program by some means...for example,
  mysum=0
  DO i=1, 3
    mysum=mysum+1
  END DO
  k=mysum

  REAL, DIMENSION(k, k**2) :: c

  WRITE(*,*) "k=", k
  WRITE(*,*) "k**2=", k**2
  WRITE(*,*)
  DO i=1,size(c,1)
    WRITE(*,"(100(3X,F3.1))") (c(i,j), j=1,size(c,2))
  END DO
END PROGRAM mytest

我试图创建的一点是我想创建一个数组 c k -by - k ** 2 的大小, k 仅由代码内的其他计算确定; k 在一开始就不知道。

The point that I am trying to make is that I would like to create an array c that is k-by-k**2 in size, and k is only determined by other computations within the code; k is not known at the very beginning.

但是,上面的代码在编译时给了我下面的错误信息:

But, the above code gives me the following error message at compile time:

mytest.f90:13.31:

  REAL, DIMENSION(k, k**2) :: c
                               1
Error: Unexpected data declaration statement at (1)

我的代码中的第13行是我最终声明 c : REAL,DIMENSION(k,k ** 2):: c

where line 13 in my code is the line where I finally declare c: REAL, DIMENSION(k, k**2) :: c.

另一方面,如果我反而声明 k 并在前面指定了它的尺寸,那么

On the other hand, if I instead declare k and specify its dimensions upfront,

PROGRAM mytest
  IMPLICIT NONE
  INTEGER :: i, j, k, mysum
  REAL, DIMENSION(3,9) :: c

  ! Let array c be a k-by-k**2 array
  ! Determine k within the program by some means...for example,
  mysum=0
  DO i=1, 3
    mysum=mysum+1
  END DO
  k=mysum

  WRITE(*,*) "k=", k
  WRITE(*,*) "k**2=", k**2
  WRITE(*,*)
  DO i=1,size(c,1)
    WRITE(*,"(100(3X,F3.1))") (c(i,j), j=1,size(c,2))
  END DO
END PROGRAM mytest

我得到正确的输出:

I get the correct output:

 k=           3
 k**2=           9

   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0

但是,由于我事先不知道 k ,所以我无法在我的实际代码中完全实现。有没有一种方法可以在开始时声明数组 c ,而无需指定其尺寸,然后在 k 是已知的吗?

But, since I don't know k beforehand, I can't do exactly this in my actual code. Is there some way to "declare" the array c initially, without specifying its dimensions, and then later specify the dimensions once the value of k is known?

推荐答案

您想使用可分配数组:

You want to use allocatable arrays:

PROGRAM mytest
  IMPLICIT NONE
  INTEGER :: i, j, k, mysum
  REAL, DIMENSION(:,:), allocatable :: c   !<-  c is allocatable, rank 2

  ! Let array c be a k-by-k**2 array
  ! Determine k within the program by some means...for example,
  mysum=0
  DO i=1, 3
    mysum=mysum+1
  END DO
  k=mysum

  WRITE(*,*) "k=", k
  WRITE(*,*) "k**2=", k**2
  WRITE(*,*)

  allocate(c(k,k**2))                  ! <-- allocate array c with supplied shape

  DO i=1,size(c,1)
    WRITE(*,"(100(3X,F3.1))") (c(i,j), j=1,size(c,2))
  END DO

  deallocate(c)                        ! <-- deallocate when done
END PROGRAM mytest

这篇关于在Fortran 90中,数组维必须事先声明吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆