0

I am trying to understand (and reproduce) the following array declaration in a fortran code segment I am editing

real, dimension (-posint:) :: test_array

Here, posint is a positive integer. I am confused about how the negative indexing syntax works, as I have attempted to reproduce with a MWE, but it failed to compile:

program Array_Games

    implicit none

    integer, dimension(-3:,3) :: test_array

    test_array = reshape((/ 1, 2, 3, 4, 5, 6, 7, 8, 9 /), shape(test_array))

end program Array_Games

The compilation error is: Error: Bad array specification for assumed shape array at (1).

However, changing the array index declaration slightly in test_array compiles successfully:

program Array_Games

    implicit none

    integer, dimension(-3:-1,3) :: test_array

    test_array = reshape((/ 1, 2, 3, 4, 5, 6, 7, 8, 9 /), shape(test_array))

end program Array_Games

Therefore, the question is: how does the array declaration, here,

real, dimension (-posint:) :: test_array

work (i.e. compile without error)? I would have expected it to look more like

real, dimension (-posint:another_number) :: test_array

where another_number is another integer.

Ian Bush
  • 6,996
  • 1
  • 21
  • 27
Physics314
  • 341
  • 1
  • 2
  • 9
  • Can you clarify what you mean by __how does the array declaration, here, work?__ If you're getting an error message, then the compiler appears to be working. The definition of an `explicit-array-spec` is `[lower-bound :] upper-bound`, which your code does not match. – steve Feb 02 '21 at 00:53
  • Clarified in question. Declarations of the sort ``real, dimension (-posint:) :: test_array`` are used 1000s of time in the code. This is a well-benchmarked code that has been running for ~ 20 years, so I know it works! I just don't understand these variable declarations. – Physics314 Feb 02 '21 at 01:03
  • 1
    It would be helpful to know, which line the error message flags. Do you know the difference between `explicit-array-spec` and `assumed-shaped-spec`? `test_array` is not a dummy argument, so it cannot be an assumed-shaped array. So, you're dealing with an explicit-shape array. You need to specify an upper bound. – steve Feb 02 '21 at 01:25
  • The fact that ``test_array`` is actually a dummy variable might be the issue: it's a dummy variable (with intent (in)) in a subroutine. I omitted this detail b/c I thought it was irrelevant, plus I wrote my own code snippet with a subroutine and an unspecified upper index, and it still failed to compile. I do not know the difference between ``explicit-array-spec`` and ``assumed-shaped-spec``, but will investigate; I'm guessing that ``assumed-shaped-spec`` can have an unspecified upper bound? If so, I will try to write my own MWE again. – Physics314 Feb 02 '21 at 01:38
  • What compiler are you using, including version number? Exactly which flags are you compiling with? Have you tried adding the appropriate flags to enforcer standards compliance? – Ian Bush Feb 02 '21 at 09:39
  • @jack Yes, that perfectly answers my question. Thank you. – Physics314 Feb 02 '21 at 13:07
  • @IanBush Compiling with gcc version 10.2.0 (Homebrew GCC 10.2.0) for the toy code I wrote above. I am not compiling with any flags, compile command is simply: gfortran Array_Games.f90. Which flags do you recommend I add? – Physics314 Feb 02 '21 at 13:07
  • 1
    -Wall -Wextra -std=f2008 for starters – Ian Bush Feb 02 '21 at 13:58

0 Answers0