mort
mort

Reputation: 13618

Too many characters in a line create "Invalid form of array reference" error?

This code:

uhelp(2:100-1,2:100-1) = (1.000000-omega)*u(2:99,2:99)+omega*0.250000*(f(2:99,2:99)+u(2-1:100-1-1,2:100-1)+u(2:100-1,2-1:100-1-1))

gives me the following error when compiled with: gfortran -o foo foo.f90

)+omega*0.250000*(f(2:99,2:99)+u(2-1:100-1-1,2:100-1)+u(2:100-1,2-1:100-1-1
                                                                       1                                                         
Error: Invalid form of array reference at (1)

I investigated the array indices and after trying a few things found to my surprise that by simply shortening the length of the statement I can get rid of the error. E.g., this works:

uhelp(2:100-1,2:100-1) = u(2:99,2:99)+omega*0.250000*(f(2:99,2:99)+u(2-1:100-1-1,2:100-1)+u(2:100-1,2-1:100-1-1))

(Notice that the first term right after the equal sign was removed)

The Fortran code is generated by my own compiler (it parses some toy language to fortran 90 and does some vectorization on the way, if possible. That's why the code looks like it does). I can't easily rewrite the input file so that shorter statements are generated. Is there a certain limit of how many characters the fortran compiler can process per line? If yes, how can I circumvent this problem? I don't know much about fortran, so I might be on the wrong track here.

Fortran version: GNU Fortran (Ubuntu/Linaro 4.7.3-2ubuntu4) 4.7.3

Disclaimer: This problem arose while doing a homework assignment.

Upvotes: 2

Views: 1500

Answers (1)

The Fortran standard clearly says, that the length of the line is limited to 132 characters. Gfortran issues a warning for this, but you didn't show it.

You can always break the line and use the continuation character & on the end on the line and continue the present statement on the next line.

I would generally not recommend to use lines longer than 80 or 100 characters. It helps readability and yoo that you can have two files opened in the editor and see the full content.

Upvotes: 4

Related Questions