(j3.2006) C1x standard
Bill Long
longb
Fri Dec 23 10:03:39 EST 2011
On 12/23/11 1:20 AM, Van Snyder wrote:
> Bill Long wrote:
>> The LEN=1 restriction does not apply if the argument is passed by
>> descriptor. How else do you propose that the length should be
>> communicated to the C function?
>
> I would expect that if the length is a constant, or determined by the C
> convention of ending with c_null, it isn't communicated to the C function.
>
I would assume that the C function is expecting a null-terminated
string. Kind=c_char does not help you with the null termination.
character(kind=c_char) is a Fortran character string (no null terminator
assumed) that uses the same encoding for each character as the companion
C compiler. It is still a Fortran character variable. You have to
manually add the null termination in or before the call. The point is
that since you have to do that anyway, you could do the kind conversion
at the same time if (in the very unlikely case) it is necessary.
Maybe a scheme like this:
integer,parameter :: n = 20
integer :: k
character(len=n) :: value
character(kind=c_char, len=n+1) :: cvalue
...
cvalue = value
k = len(value) + 1 ! of len_trim(value)+1 depending on the usage
cvalue(k:k) = C_NULL_CHAR
call cfun (cvalue)
...
As was pointed out previously, the focus on get_command_argument here is
a red herring. The string you are passing to a C function will have come
from somewhere else is the vast majority of cases. You need to play the
"add the null" game in all of the cases. If this is a legacy code, it
is unlikely that all of those other Fortran character values are
declared kind=c_char, and unlikely someone will go through the code to
change all of the declarations. Especially since there are a lot of
important places where default kind character is required, and such a
conversion would cause many problems in exchange for helping with few.
>>
>> Checking the man page for pxfopen, I see some arguments of type
>> character. Certainly this is Fortran default character, since these
>> interfaces are from the f77 era when there was no other kind.
>>
>
> They apparently haven't been updated to "some other implementation
> defined type," so the problem remains: how does one pass a characgter
> string gotten from get_command_argument to them?
>
What problem? The pxfopen routine expects a Fortran default character
actual argument. The get_command_argument intrinsic returns a Fortran
default character
value. Exact match. No problem.
Cheers,
Bill
--
Bill Long longb at cray.com
Fortran Technical Support & voice: 651-605-9024
Bioinformatics Software Development fax: 651-605-9142
Cray Inc./Cray Plaza, Suite 210/380 Jackson St./St. Paul, MN 55101
More information about the J3
mailing list