Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6068913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:45:58+00:00 2026-05-23T09:45:58+00:00

Is it necessary to declare array dimensions before any other code? For example, I

  • 0

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

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)

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

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

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:45:58+00:00Added an answer on May 23, 2026 at 9:45 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Our code was written based on the example from Google Gears' own docs. We're
I've the following code in my app. MyEventHandler handler = null; //Declare the handler
Is it necessary or advantageous to write custom connection pooling code when developing applications
Are CDATA tags ever necessary in script tags and if so when? In other
Is it necessary for setter methods to have one argument? Usually setter methods accept
Are hints really necessary for every sql statement? We have a dba who is
Do you have to declare methods replacing a pure virtual function in a base
I have a simple question. Why is it necessary to consider the terminating null
I have a simple question. Why is it necessary to consider the terminating null
i was following an ACL tut. which has used this piece of code. class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.