Strings

Programs written in C expect strings to be terminated with the null character ('\0'). But HP Fortran programs pass a hidden length parameter to indicate the end of a string argument. Thus, if you want to pass a string from HP Fortran to a C language function, you must explicitly append the null to the string and suppress the hidden length parameter. The $HP$ ALIAS directive enables you to pass the string from Fortran to C. For example, consider the following routine:

Example 31 Example 9-1 pr_str.c

void c_rout(char *s)

{

printf(“%s\n”, s);

}

The ALIAS directive in the following program enables the string to be passed to c_rout:

Example 32 Example 9-2 pass_str.f90

PROGRAM main

!$HP$ ALIAS c_rout(%REF) CHARACTER(LEN=10) name name = 'Charlie'

!Append a null to the string so that C can handle it properly CALL c_rout(name//char(0))

END PROGRAM main

Here are the command lines to compile and link both files, and to execute the program, along with the output from a sample run:

$ cc -Aa -c pr_str.c

$ f90 pass_str.f90 pr_str.o $ a.out

Charlie

For more information

For detailed information about the %REF and %VAL built-in functions, see the HP Fortran Programmer’s Reference.

$HP$ CHECK_OVERFLOW

The $HP$ CHECK_OVERFLOWdirective generates code to trap when an overflow occurs in integer arithmetic. By default, integer overflow is ignored.

Syntax

!$HP$ CHECK_OVERFLOW INTEGER [ON OFF]

ON

causes the compiler to generate code to trap integer overflow exceptions.

OFF

causes the compiler not to generate code to trap integer overflow exceptions.

Description and restrictions

If you use $HP$ CHECK_OVERFLOWwith the ONstatement, you can cause your program to ignore the overflow, abort on the overflow, or branch to a trap subroutine. If this directive is not used, the ONstatement has no effect on integer overflow errors.

This directive can appear anywhere in your program. It stays in effect until a subsequent $HP$ CHECK_OVERFLOWdirective changes the status.

126 Using Fortran directives

Page 126
Image 126
HP UX Fortran Software Strings, For more information, Example 31 Example 9-1 prstr.c, Example 32 Example 9-2 passstr.f90