Array First Index in Fortran

后端 未结 2 982
半阙折子戏
半阙折子戏 2021-01-14 09:33

I thought the first index of an array in Fortran is 1. But why does this code work? (Code is a modified segment of Wavewatch, http://polar.ncep.noaa.gov/waves/wavewatch/)

相关标签:
2条回答
  • 2021-01-14 10:08

    You thought it wrong. Arrays can be declared to start from any integer.

           REAL SIG(42:58)
    
    0 讨论(0)
  • 2021-01-14 10:35

    As you've already been told Fortran array indexing is, by default, 1-based but the programmer can choose any integer within the range of the integer kind used for index values. There is, though, another wrinkle of which you should be aware. Fortran doesn't by default, either at compile-time (where it would be impossible in many cases) or at run-time (possible but expensive), check that array index expressions are in bounds.

    There is a lot of Fortran code in the wild with this problem and I've come across cases where a program has worked, apparently correctly, for many years without this being spotted. Use your compiler's options to create a version of the program which checks array bounds expressions at run-time, run it and see what happens.

    Or, as you've already been told, SIG may have been declared with 0 as its lowest index.

    0 讨论(0)
提交回复
热议问题