This is the mail archive of the
fortran@gcc.gnu.org
mailing list for the GNU Fortran project.
Re: 16 bits char string from C to FORTRAN
- From: Tobias Burnus <burnus at net-b dot de>
- To: Americo Barbosa da Cunha Junior <americo dot cunhajr at gmail dot com>
- Cc: "'fortran at gcc dot gnu dot org'" <fortran at gcc dot gnu dot org>
- Date: Fri, 24 Apr 2009 09:50:55 +0200
- Subject: Re: 16 bits char string from C to FORTRAN
- References: <b7e3a9c0904231550x28217ab0v4a6abd0b02132696@mail.gmail.com> <b7e3a9c0904231558g25f215la8802b76bb0e0e41@mail.gmail.com>
Americo Barbosa da Cunha Junior wrote:
> I'm calling a FORTRAN subroutine in a ANSI C program. The subroutine
> has the following prototype:
> FORTRAN:
>
> SUBROUTINE CKINIT (LENIWK, LENRWK, LENCWK, LINC, LOUT, ICKWRK,
> 1 RCKWRK, CCKWRK, IFLAG)
>
> The char* string passed to CKINIT must be at least 16 bits
> (CHARACTER*16) or the program is finished with the error message:
>
> CHARACTER LENGTH OF CCKWRK MUST BE AT LEAST 16
>
As written before by Steve: "CHARACTER*16" is an old way to write
"CHARACTER(len=16)" which denotes a string of 16 characters, each of
them one-byte wide (8 bits).
Thus you can pass a "char *" to the subroutine.
NOTE: Fortran does not have the concept of zero ('\0') terminated
strings; thus if you need to read the string from C, you should take
care of the zero termination yourself.
NOTE 2: If the string in Fortran is not written as
CHARACTER*16
but as CHARACTER(*), most Fortran compilers expect an additional "int *"
argument with the string length, which follows all other arguments, i.e.
in your case the IFLAG argument. One can still pass the stringlength for
CHARACTER*16, but it also works if one does not do so.
Tobias