24.05.2014 Views

XL Fortran Enterprise Edition for AIX : User's Guide - IBM

XL Fortran Enterprise Edition for AIX : User's Guide - IBM

XL Fortran Enterprise Edition for AIX : User's Guide - IBM

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Common blocks that are declared THREADPRIVATE can be accessed using a C<br />

global variable that is declared as THREADPRIVATE, using C <strong>for</strong> <strong>AIX</strong> 4.5 or later.<br />

Passing Character Types between Languages<br />

One difficult aspect of interlanguage calls is passing character strings between<br />

languages. The difficulty is due to the following underlying differences in the way<br />

that different languages represent such entities:<br />

v<br />

v<br />

The only character type in <strong>Fortran</strong> is CHARACTER, which is stored as a set of<br />

contiguous bytes, one character per byte. The length is not stored as part of the<br />

entity. Instead, it is passed by value as an extra argument at the end of the<br />

declared argument list when the entity is passed as an argument.<br />

Character strings in C are stored as arrays of the type char. A null character<br />

indicates the end of the string.<br />

v<br />

Note: To have the compiler automatically add the null character to certain<br />

character arguments, you can use the “-qnullterm Option” on page 206.<br />

Pascal’s character-variable data types are STRING, PACKED ARRAY OF<br />

CHAR, GSTRING, and PACKED ARRAY OF GCHAR. The STRING data type<br />

has a two-byte string length that is usually aligned on a half-word boundary<br />

followed by a set of contiguous bytes, one character per byte. The dynamic<br />

length of the string can be determined using the predefined Pascal function<br />

LENGTH. Packed arrays of CHAR, like <strong>Fortran</strong>’s CHARACTER type, are stored<br />

as a set of contiguous bytes, one character per byte.<br />

If you are writing both parts of the mixed-language program, you can make the C<br />

routines deal with the extra <strong>Fortran</strong> length argument, or you can suppress this<br />

extra argument by passing the string using the %REF function. If you use %REF,<br />

which you typically would <strong>for</strong> pre-existing C routines, you need to indicate where<br />

the string ends by concatenating a null character to the end of each character string<br />

that is passed to a C routine:<br />

! Initialize a character string to pass to C.<br />

character*6 message1 /’Hello\0’/<br />

! Initialize a character string as usual, and append the null later.<br />

character*5 message2 /’world’/<br />

! Pass both strings to a C function that takes 2 (char *) arguments.<br />

call cfunc(%ref(message1), %ref(message2 // ’\0’))<br />

end<br />

For compatibility with C language usage, you can encode the following escape<br />

sequences in <strong>XL</strong> <strong>Fortran</strong> character strings:<br />

Table 25. Escape Sequences <strong>for</strong> Character Strings<br />

Escape<br />

Meaning<br />

\b Backspace<br />

\f Form feed<br />

\n New-line<br />

\t Tab<br />

\0 Null<br />

\’ Apostrophe (does not terminate a string)<br />

\" Double quotation mark (does not terminate a string)<br />

\ \ Backslash<br />

Interlanguage Calls 351

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!