Issue with Character Arrays

Hi all,

I have been encountering an error with a FORTRAN program that I was able to narrow down to possibly a compiler issue. Here is a short example code that throws the error:

program test
implicit none

character(len=12) :: test1(2),test2(2)

test1=(/'1234567890','123456789'/)
test2=(/'123456789','1234567890'/)

print *,'Test 1 ',test1
print *,'Test 2 ',test2

On my machine, I get the following output:

Test 1 1234567890 123456789
Test 2 123456789 123456789

Now, maybe there is some obscure FORTRAN rule (not the rarest thing in the world) that causes this to occur–that when I have a character array with more than one element and the elements are of different length, the the final element’s trailing 0 is removed.

I use these arrays as flags during loops, so I am luckily able to use test1 instead of test2. For rigor, I use gfortran to compile the same program and get this output when running:

Test 1 1234567890 123456789
Test 2 123456789 1234567890

Anyone have any suggestions or experiencing the same problem? For the record, I am using pgf90 11.10-0.

Hi,

I would need to check with the Fortran language experts here, but I suspect that the strings of different character lengths may be undefined behavior.

If you add a space to the end of the first string of test2, the PGI output will do what you expect:

cparrott ~ $ pgf90 -o testout testout.f90
cparrott ~ $ ./testout
 Test 1 1234567890  123456789
 Test 2 123456789   1234567890

For what it’s worth, gfortran 4.8.2 now flags your example with an error:

cparrott ~ $ gfortran -o testout testout.f90
testout.f90:6.21:

test1=(/'1234567890','123456789'/)
                     1
Error: Different CHARACTER lengths (10/9) in array constructor at (1)
testout.f90:7.20:

test2=(/'123456789','1234567890'/)
                    1
Error: Different CHARACTER lengths (9/10) in array constructor at (1)

Hope this helps.

Best regards,

+chris