4

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/)

DO KINDEX=0, TOTAL+1
NUM    = NUM * SCALE
SIG (KINDEX) = NUM
END DO
1
  • 3
    The default is 1-based indexing but it doesn't have to be - check how SIG is declared. Commented Jun 26, 2014 at 6:18

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

       REAL SIG(42:58)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.