FIX: Concatenation of Substring Gives Incorrect ResultsLast reviewed: September 11, 1997Article ID: Q70243 |
4.00 4.01 4.10 5.00 5.10 | 4.10 5.00 5.10
MS-DOS | OS/2kbtool kbfixlist kbbuglist The information in this article applies to:
SYMPTOMSPrograms compiled with Microsoft FORTRAN versions 4.0, 4.01, 4.1, and 5.0 that concatenate substrings having a variable for a substring index, can give incorrect results or hang the machine when executed under MS-DOS, or result in a protection violation when executed under OS/2.
CAUSEThe problem is usually observed when the concatenation occurs inside a function call, or inside OPEN or IF statements.
RESOLUTIONTo avoid this problem, assign the concatenated expression to a temporary character variable and use the temporary variable in the program.
STATUSMicrosoft has confirmed this to be a problem in FORTRAN versions 4.0, 4.01, 4.1, 5.0, and 5.1. This problem was corrected in FORTRAN PowerStation.
MORE INFORMATIONThe following sample programs illustrate the problem:
In an IF Statement
character r*2 /'AA'/ n=0 if ('BBB' .gt. r(1:n+2)//'A') then ! The IF statement should ! cause 'YES' to be printed, ! however 'NO' is printed ! instead. write (*,*) 'YES' else write (*,*) 'NO' endif if ('BBB' .gt. r(1:2)//'A') then write (*,*) 'YES' else write (*,*) 'NO' endif end In a Function
character*4 a character*1 b a = 'cdef' b = 'd' i = len(a) print *, i j = index('ab'//a(1:i),b) ! The INDEX function should cause '4' ! to be printed, however other values ! are generated instead. print *, j endAssigning the concatenated expression to a temporary character variable and using the temporary variable in the IF statement or function call will prevent the problem from occurring, as illustrated by the following sample programs:
In an IF Statement
character r*2 /'AA'/ character str*13 ! Declare a temporary variable and str = r(1:n+2)//'A' ! assign the concatenated expression ! to it. n = 0 if ('BBB' .gt. str) then ! Use the temporary variable in ! the IF statement. write (*,*) 'YES' else write (*,*) 'NO' endif if ('BBB' .gt. r(1:2)//'A') then write (*,*) 'YES' else write (*,*) 'NO' endif end In a Function
character*4 a character*1 b character*12 str ! Declare a temporary variable. a = 'cdef' b = 'd' i = len(a) print *, i str = 'ab'//a(1:i) ! Assign the concatenated expression ! to the temporary variable. j = index(str,b) ! Use the temporary variable in the ! index function. print *, j end |
Additional reference words: 5.00
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |