HP Fortran Programmer's Guide (September 2007)
Calling C routines from HP Fortran
Data types
Chapter 8 179
float real;
float imag;
}COMPLEX;
/* returns the square of the complex argument */
COMPLEX sqr_complex(COMPLEX cmx_val)
{
COMPLEX result;
float a, b;
/* copy both parts of the complex number into locals */
a = cmx_val.real;
b = cmx_val.imag;
/* square the complex number and store the results into
* the return variable
*/
result.imag = 2 * (a * b);
a = a * a;
b = b * b;
result.real = a - b;
return result;
}
Below are the command lines to compile, link, and execute the program, followed by the
output from a sample run.
$ cc -Aa -c sqr_complex.c
$ f90 pass_complex.f90 sqr_complex.o
$ a.out
C will square this complex number: (2.5,3.5)
The squared result is: (-6.0,17.5)
Derived types
Although the syntax of Fortran's derived types differs from that of C's structures, both
languages have similar default packing and alignment rules. HP Fortran uses the same
packing rules and alignments when laying out derived-type objects in memory that HP C uses
for structures.
Pointers
Although the Fortran pointer differs in some respects from the C pointer, a pointer passed by
Fortran to a C function looks and acts the same as it does in C. The only precaution is that,
when the pointer is to an array (which will almost always be the case), the two languages
store and access arrays differently; see “Arrays” on page 186.