I'm an absolute beginner to Fortran95 so I apologise for the simplicity of this question. I want to be able define the dimensions of an array in terms of integers but I get the error
'The module or main program array 'arrayx' at (1) must have constant shape'
and 'Variable 'y' cannot appear in the expression at (1)' (same error for x). Is it simply not possible to define the dimensions in terms of variables or am I just missing something obvious.
program arrayForm
implicit none
integer, save :: x=40, y=2
real, dimension(x,y) :: arrayX
end program arrayForm
Module or main program array must have constant shape error in Fortran Ask Question Asked4 years, 5 months ago Active3 years, 9 months ago Viewed4k times 3 2 An integer variable declared in the module is used as a global variable to define the size of related arrays in the program.
Module or main program array must have constant shape error in Fortran - Stack Overflow An integer variable declared in the module is used as a global variable to define the size of related arrays in the program. The size of program varies, so the size of array is a variable but not a Stack Overflow About Products For Teams
Elemental procedures are specified with scalar dummy arguments that may be called with array actual arguments. In the case of a function, the shape of the result is the shape of the array arguments. Most intrinsic functions are elemental and Fortran 95 extends this feature to non-intrinsic...
The lowest address corresponds to the first element and the highest address to the last element. Arrays can be one- dimensional (like vectors), two-dimensional (like matrices) and Fortran allows you to create up to 7-dimensional arrays. Arrays are declared with the dimension attribute.
Variables with the save
attribute are not constants and the compiler complains with justification. In your snippet replace that attribute with the parameter
attribute which makes them constant and, at compile-time, the array can be fixed in size and the compiler shouldn't complain.
As you have employed it the save
attribute is redundant. When applied to variables which may go out of scope, such as variables inside the scope of a subroutine or function, it requires that the value of the variable is saved from one invocation of the sub-scope to the next. But since your variables are declared in your program scope they won't ever go out of scope during execution.
Note also that one of Fortran's gotchas for new (and old) programmers is that variables which are initialised in their declaration acquire the save
attribute automatically. So, in a subroutine your line
integer, save :: x=40, y=2
could simply be written
integer :: x=40, y=2
Personally I tend to use the save
attribute so that my intentions when I write a subroutine are clear to me later.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With