| Re: C Interoperability - Strings |
|
 |
|
 |
|
 |
|
 |
Group: comp.lang.fortran · Group Profile
Author: nospamnospam Date: Sep 1, 2008 10:04
Gary Scott sbcglobal.net> wrote:
>> Also you are aware that to be interoperable Fortran Character
>> variables must have Len = 1 ?
>
> No, I only use F95 compilers at present (CVF, LF95, Absoft F90) that
> interoperate with normal character buffers of whatever length as long as
> you insert a null character at the end of the trimmed length. Are you
> saying that I must use an array to designate a string? Boy that would
> be inconvenient.
No, that is *NOT* the implication, although Ian might well mistakenly
think so. That seems to be developing as a FAQ for the C interop stuff.
The developers of the standard aren't quite so dense as to not have
figured out how inconvenient that would be. That exact issue got very
specific attention during development. There is even a special-case rule
to facilitate it. (It turned out not to take too much of a special case,
but it did require one somewhat strange historical rule to be extended
from default character to also apply to character(kind=c-kind), even
though it doesn't apply to all character kinds).
See Note 15.23 in F2003, which gives an example of exactly this and
attempts to explain why it works.
The (very) short version is that an actual argument to a bind(c)
procedure does not have to be interoperable. Only the dummy arguments
have to be interoperable. The normal rules for argument agreement
(including the one historical special case) then apply to what the
actual arguments may be.
This is the same reason that, for example, you can pass an allocated
allocatable array or associated pointer array as an actual argument,
even though those things aren't interoperable. Those are valid actual
arguments for non-allocatable, non-pointer dummy arguments.
The special-case rule for strings is that you don't have to have length
agreement in all cases. In the exceptional cases, argument association
is done as character-by-character argument sequence association. That's
perhaps a complicated way of saying that it "just works". (But no, it
doesn't involve automatic appending or removal of null characters).
--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
|