How FORTRAN Maintains String Lengths (34972)



The information in this article applies to:

  • Microsoft FORTRAN Compiler for MS-DOS 4.0
  • Microsoft FORTRAN Compiler for MS-DOS 4.01
  • Microsoft FORTRAN Compiler for MS-DOS 4.1
  • Microsoft FORTRAN Compiler for MS-DOS 5.0
  • Microsoft FORTRAN Compiler for MS-DOS 5.1
  • Microsoft FORTRAN compiler for OS/2 4.1
  • Microsoft FORTRAN compiler for OS/2 5.0
  • Microsoft FORTRAN compiler for OS/2 5.1

This article was previously published under Q34972

SUMMARY

In ANSI FORTRAN, an application can pass character variables of varying lengths to a subprogram by using the CHARACTER*(*) type declaration in the subprogram. To use this technique, the subprogram must have access to the strength length. In Microsoft FORTRAN, this information is stored in an array of two-byte integers in a memory location that the __FCclenv global variable points to. When a calling program calls a subprogram that has character arguments, the string length table is updated with the lengths of the parameter strings in the order in which the arguments appear in the call or function reference. The table reflects the length of each string argument because the calling routine does not know in advance if the called subprogram declares the character data as a fixed length or varying length string. The first position in the table is reserved for the size of the string returned from a character function. If the subprogram does not return a character value, the first position in the table is undefined and contains an arbitrary value.

Because the __FCclenv global variable contains the address of the table of string lengths, the variable itself does not provide any useful information. View the string length table by dereferencing the value. Because FORTRAN does not define a method to deference a pointer, another method is required. If an INTERFACE statement declares a subprogram to receive an argument by value and the variable in the subprogram is defined without the [value] attribute, the subprogram receives the dereferenced value which it can return to its caller. The subroutine must be declared in a separate file to avoid a compiler error for the intentional mismatch in calling methods.

NOTE: __FCclenv is not defined in FORTRAN PowerStation.

MORE INFORMATION

The following example demonstrates viewing the contents of the FORTRAN string table.

Sample Code #1

C Compiler options required: None
C The function deref dereferences the address contained in __FCclenv.

      INTERFACE TO INTEGER*2 FUNCTION DEREF(ADDRESS)
      INTEGER ADDRESS[VALUE]
      END

      CHARACTER*17 A
      CHARACTER*13 B
      CHARACTER*30 STRINGFUN

      A = 'AAAAAAAAAAAAAAAA '
      B = 'BBBBBBBBBBBBB'
      PRINT*, STRINGFUN(A,B)
      END

      CHARACTER*(*) FUNCTION STRINGFUN(A,B)
      CHARACTER*(*) A,B

C Declare the __FCclenv global variable as follows.

      INTEGER*4 FCCLENV[ALIAS:'__FCCLENV',EXTERN]
      INTEGER*2 DEREF

C The following line prints the first 3 positions in the string
C length table. Because __FCclenv is an address, adding 2 moves the
C address to the next element in the table.

      PRINT*, DEREF(FCCLENV), DEREF(FCCLENV+2), DEREF(FCCLENV+4)
      STRINGFUN = A//B
      RETURN
      END
				
The following function dereferences __FCclenv and returns the integer value in the string length table. Place this code into a separate file from Sample Code #1.

Sample Code #2

C Compiler options required: None

      INTEGER*2 FUNCTION DEREF(ADDRESS)
      INTEGER ADDRESS
      DEREF = ADDRESS
      RETURN
      END
				

Modification Type:MajorLast Reviewed:12/1/2003
Keywords:KB34972