[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

FORTRAN character data (was: Re: FORTRAN common blocks)



On 19-Jan-2006, Brett Viren wrote:

| Eric S Fraga <ucecesf@ucl.ac.uk> writes:
| 
| >
| > All arguments are passed by reference in Fortran.
| 
| To be pedantic, only *explicit*, non-array arguments are passed by
| reference.
| 
| For example, when passing a character string, the pointer is passed by
| value and there is an implicit string length which is also passed by
| value.
| 
| This is shown in this C wrapper for the CERNLIB subroutine HBOOK1:
| 
| void hbook1(int id, char *title, int nbins, float min, float max)
| {
|     void hbook1_(int*,char*,int*,float*,float*,float*,int) ;
|     float vmx = 0.0;
|     hbook1_(&id, title, &nbins, &min, &max, &vmx, strlen(title));
| }
| 
| Here, "hbook1_" is the mangled symbol name of the actual FORTRAN
| subroutine HBOOK1.  BTW, this mangling depends on the FORTRAN compiler
| and what options were given but trailing underscore is common for g77.

This is not universally true.  The particular method used for passing
character arguments in Fortran is compiler/system dependent.  The
convention you describe (appending a long int at the end of the
argument list for each character argument) is used by g77/f2c and some
other Unix compilers.  CRAY and VMS compilers use a descriptor (the
pointer to the data and the length are passed in a structure of some
kind).  I believe M$ Fortran puts the length as an additional int
argument just after each character argument.

GNU Autoconf has some macros to help with determining how underscores
are used.  GNU Octave has some macros that can help with passing
character data to/from Fortran and C/C++ (if you are interested, they
are in src/libcruft/misc/f77-fcn.h).  It's a bit messy, but with
some care it is possible to accomodate all of these methods in your
code.

jwe



Reply to: