Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
795 views
in Technique[技术] by (71.8m points)

arrays - Difference between "character*10 :: a" and "character :: a(10)"

Trying to refresh my Fortran 90 knowledge for a project, I have run into some oddity when using internal files. Consider the example code:

! ---- internal_file_confusion.f90 ----
program internal_file_confusion
  implicit none 

  character*40 :: string1
  character :: string2(40)

  write(string1, *) "Hello World 1"
  write(*,*) "string1 = ", string1

  write(string2, *) "Hello World 2"
  write(*,*) "string2 = ", string2

end program 

which when compiled with gfortran crashes, writing to STDOUT

 string1 =  Hello World 1                          
At line 10 of file e:/Daten/tmp/fortran-training/internal_file_confusion.f90
Fortran runtime error: End of record

When declared with the *length notation the character array can be used for internal write, but not when declared with the name(length) notation. Furthermore I noticed that the *length notation seems to be allowed only for character arrays, while it is forbidden with an error message like

Error: Old-style type declaration INTEGER*40 not supported at (1)

for other data types.

What is the difference between these notations and why does it affect use as internal files?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

character*40 :: string is a character string of length 40

character :: string*40 is the same

character(len=40) :: string is also a character string of length 40

character :: string(40) is an array of 40 character strings of length 1

character*40 :: string(40) is an array of 40 character strings of length 40

character :: string(40)*40 is the same

character(len=40) :: string(40) is an array of 40 character strings of length 40

Your second internal writes fails, because it writes to the first string in the array string2. The first string string2(1) is just 1 character long and that is too short. For that reason you get the end of record error condition, the message is too long for the supplied string.

Internal writes treat array elements as separate records (similar to separate lines). One can utilize arrays of string in internal writes if one has more records (lines) to write into the array.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...