Re: C Interoperability - Strings
  Home FAQ Contact Sign in
comp.lang.fortran only
 
Advanced search
POPULAR GROUPS

more...

 Up
Re: C Interoperability - Strings         

Group: comp.lang.fortran · Group Profile
Author: Ian Bush
Date: Sep 1, 2008 23:55

On 1 Sep, 18:04, nos...@see.signature (Richard Maine) wrote:
> 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).
>

Ok, a day in which you learn something is never a wasted day ! Thanks
Richard,
I had read the final paragraph of section 14.2 in Metcalf, Reid and
Cohen,
where it states that for a character type interoperability "requires
the length
type parameter to be omitted or be specified by an initialization
expression
whose value is one." I had also read the penultimate paragraph of
section 14.3
where it talks about while Fortran pointers, allocatable variables and
most Fortran
arrays do not interoperate directly with C the compiler will make it
work. What
I had not twigged was this last case also applies to Character
variables,

Live and learn,

Ian
no comments
diggit! del.icio.us! reddit!