FIX: Concatenation of Substring Gives Incorrect Results

ID: Q70243


The information in this article applies to:
  • Microsoft FORTRAN for MS-DOS, versions 4.0, 4.01, 4.1, 5.0, 5.1
  • Microsoft FORTRAN for OS/2, versions 4.1, 5.0, 5.1


SYMPTOMS

Programs 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.


CAUSE

The problem is usually observed when the concatenation occurs inside a function call, or inside OPEN or IF statements.


RESOLUTION

To avoid this problem, assign the concatenated expression to a temporary character variable and use the temporary variable in the program.


STATUS

Microsoft 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 INFORMATION

The 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
      end 
Assigning 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 query words: 5.00

Keywords :
Version : :4.0,4.01,4.1,5.0,5.1
Platform :
Issue type :


Last Reviewed: November 2, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.