Can equations be used in Fortran array declarations with parameters?

In summary, the statement is legitimate, but the variable y gets its value at run time, not at compile time.
  • #1
Matterwave
Science Advisor
Gold Member
3,971
328
Hi, I'm just wondering real quick, are equations allowed in array declarations? For example:

int :: y=2
int, dimension(2*y):: x

?
 
Technology news on Phys.org
  • #2
I don't think the equation you have would be permitted, as the array bounds must be INTEGER constants. However, constants declared with the PARAMETER statement are OK for array declarations, as long as the PARAMETER statement precedes the array declaration.

http://star-www.st-and.ac.uk/~spd3/Teaching/AS3013/lectures/AS3013_lecture5.pdf
 
  • #3
So even if y were a parameter, probably I still can't do that?

EDIT: I looked at a later part of the lecture and he had something like what I have except y is a parameter. So I think I'm doing OK. Thanks. :)
 
  • #4
The whole point of a parameter in Fortran is that the parameter's value is known at compile time, so the compiler knows how much memory to allocate for the array. For a variable (not a parameter), the variable gets its value at run time, I believe.

Minor point. A statement such as y = 2 is an assignment, or in this case an initialization, not an equation. It does not state that y and 2 are equal. Instead, it stores the constant value 2 in the location that is named y.

Most programming languages make distinctions between assignment and testing for equality by using different symbols. For example, Fortran uses .EQ. to test for equality, and uses = for assignment. Languages based on C, such as C++, C#, Java, and others, use == to test for equality and = for assignment. Newer versions of Fortran now support the C-style notation, in addition to the older .EQ., .LT., GT., and other spelled-out relational operators.
 
  • #5
Yes, I forgot to include that y is a parameter, and my language is lazy because I'm not a programmer really. Sorry for the confusion. :P

I just wanted to make sure that the statement

real, dimension(2*y)::x

is legitimate if y is a parameter, and Steamking's link had something just like that in it.
 

What is Fortran array declaration?

Fortran array declaration is the process of defining an array, which is a collection of data items of the same type, in Fortran programming language. It involves specifying the type of data, the size and shape of the array, and the name of the array.

How do you declare a one-dimensional array in Fortran?

To declare a one-dimensional array in Fortran, you need to use the DIMENSION statement and specify the size of the array. For example, to declare an array named "my_array" with 10 elements of type integer, you would use the statement DIMENSION my_array(10).

Can you declare arrays of different data types in Fortran?

Yes, you can declare arrays of different data types in Fortran. This is known as a heterogenous array. For example, you can declare an array that contains both integer and real numbers.

What is the maximum number of dimensions that can be declared for an array in Fortran?

The maximum number of dimensions that can be declared for an array in Fortran is seven. This means that an array can have up to seven indices to access its elements.

Can the size of an array be changed after it is declared in Fortran?

No, the size of an array cannot be changed after it is declared in Fortran. However, you can create a new array with a different size and copy the elements from the original array to the new one.

Similar threads

  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
20
Views
3K
  • Programming and Computer Science
2
Replies
62
Views
3K
  • Programming and Computer Science
Replies
4
Views
499
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
6
Views
789
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top