title: In Fortran 90, do array dimensions have to be declared beforehand?
body: <p>Is it necessary to declare array dimensions before any other code?  For example, I have written the following simplified example code:</p>
  
  <pre><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
  </code></pre>
  
  <p>The point that I am trying to make is that I would like to create an array <code>c</code> that is <code>k</code>-by-<code>k**2</code> in size, and <code>k</code> is only determined by other computations within the code; <code>k</code> is not known at the very beginning. </p>
  
  <p>But, the above code gives me the following error message at compile time:</p>
  
  <pre><code>mytest.f90:13.31:
  
    REAL, DIMENSION(k, k**2) :: c
                                 1
  Error: Unexpected data declaration statement at (1)
  </code></pre>
  
  <p>where line 13 in my code is the line where I finally declare <code>c</code>: <code>REAL, DIMENSION(k, k**2) :: c</code>.</p>
  
  <p>On the other hand, if I instead declare <code>k</code> and specify its dimensions upfront,</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I get the correct output:</p>
  
  <pre><code> 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
  </code></pre>
  
  <p>But, since I don't know <code>k</code> beforehand, I can't do exactly this in my actual code.  Is there some way to "declare" the array <code>c</code> initially, without specifying its dimensions, and then later specify the dimensions once the value of <code>k</code> is known?</p>