Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

fortran - "DATA INT / 'STRING' /" problem when compiling with gfortran

I have some old (~1995) legacy fortran code which is compiled with g77 compiler and fails on gfortran. The problem is in following lines (incompatible types conversion, character to integer):

INTEGER  CKYAN
DATA     CKYAN / 'KYAN' /

The variable CKYAN is used only once as trigger:

IF(IWVTX.EQ.CKYAN)THEN
    CALL FDCVERTEXSWITCHTOKYAN()
ENDIF

The integer IWVTX is read from input file (string 'KYAN' or some other string).

I do not have any significant experience in fortran and I fail to find any documentation on what is going on when I initialize integer variable from the string. I have checked with the g77 compiler. The following code:

  INTEGER it,ita,it1,it2,it3,it4,it5,it6
  DATA it  / 'KYAN' /
  DATA ita / 'KYAN' /
  DATA it1 / 'K' /
  DATA it2 / 'Y' /
  DATA it3 / 'A' /
  DATA it4 / 'N' /
  DATA it5 / 'O' /
  DATA it6 / 'o' /
  write(*,*) 'test', it, ita, it1, it2, it3, it4, it5, it6

produces the output:

1312905547 1312905547 538976331 538976345 538976321 538976334 538976335 538976367

So, if the strings are equal the numbers are also equal. If one letter goes next to the other, it's code is incremented by 1.

The questions are the following:

  1. What is going on here?
  2. Is there a function that does the same and available gfortran?
  3. Or is there a simple way to fix this, not touching any other files?

Can anybody help me with this? Thank you in advance.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Wow - and this was written in 1995? Eep.

As far as I can tell, this is basically Hollerith encoding, encoding character constants in integers (from back before there was a CHARACTER data type). As a quick test, setting one of those integers equal to 4HKYAN seems to give the same answer.

The reason for this here just seems to be to set a flag equal to some constant to test against afterwards. If you want to do the same thing, the modern way to do this is ckyan = transfer('KYAN',ckyan), which takes the bit representation of the character string, converts it to the format of the variable passed as it's second parameter, and returns it.

But here, it looks like the value of the named constant isn't critical as long as the values IWVTX can take on for the different cases are distinct...

By the way, you may already know about this, but the Fortran Wiki has a very handy page on Modernizing Old Fortran; it doens't cover everything (like this one, which I hadn't seen in quite this form before), but it has help for translating a lot of old, non-standard contstructs into modern Fortran.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...