How FORTRAN Maintains String LengthsLast reviewed: July 19, 1995Article ID: Q34972 |
The information in this article applies to:
SUMMARYIn 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 INFORMATIONThe following example demonstrates viewing the contents of the FORTRAN string table.
Sample Code #1C 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,BC Declare the __FCclenv global variable as follows.
INTEGER*4 FCCLENV[ALIAS:'__FCCLENV',EXTERN] INTEGER*2 DEREFC 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 ENDThe 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 #2C Compiler options required: None
INTEGER*2 FUNCTION DEREF(ADDRESS) INTEGER ADDRESS DEREF = ADDRESS RETURN END |
Additional reference words: 4.00 4.10 5.00 5.10 global array kbinf
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |