Bug 36759 - C_LOC and characters greater then one in length.
Summary: C_LOC and characters greater then one in length.
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.4.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-07-07 21:44 UTC by Scot Breitenfeld
Modified: 2008-07-09 14:24 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Scot Breitenfeld 2008-07-07 21:44:05 UTC
When I compile the code:

PROGRAM main
  USE, INTRINSIC :: ISO_C_BINDING
  IMPLICIT NONE
  CHARACTER(LEN=2), TARGET :: arg1 = '12'
  TYPE(c_ptr) :: A
  A = c_loc(arg1)
END PROGRAM main

I get the error: 

  A = c_loc(arg1)
           1
Error: CHARACTER argument 'arg1' to 'c_loc' at (1) must have a length of 1

I'm not sure where this restriction comes from (it's not part of the standard). All the other fortran compilers (at least g95, ifort, and pgf90) support character LEN >1. Actually they support CHARACTER(LEN=*) when declared in a subroutine.
Comment 1 Tobias Burnus 2008-07-07 22:41:42 UTC
>   A = c_loc(arg1)
>            1
> Error: CHARACTER argument 'arg1' to 'c_loc' at (1) must have a length of 1
> 
> I'm not sure where this restriction comes from (it's not part of the standard).

One could argue that one should allow it as vendor extension, however, the restriction is in the standard:

"Interoperability of intrinsic types"
"A Fortran intrinsic type with particular type parameter values is interoperable with a C type if the type and kind type parameter value are listed in the table on the same row as that C type; if the type is character, interoperability also requires that the length type parameter be omitted or be specified by an initialization expression whose value is one."

For interoperability one thus should use instead of
  character(len=2) :: str
rather
  character(len=1,kind=c_char) :: str(2)

(This is also the case for dummy arguments of BIND(C) procedures, though there one can as pass "a string" as actual argument, cf. "storage sequence".)
Comment 2 Scot Breitenfeld 2008-07-08 17:43:52 UTC
Subject: Re:  C_LOC and characters greater then one in
 length.

My mistake, I did not see that in the standard.

It still would be nice however to have the non-standard feature for 
characters of LEN > 1.

burnus at gcc dot gnu dot org wrote:
> ------- Comment #1 from burnus at gcc dot gnu dot org  2008-07-07 22:41 -------
>   
>>   A = c_loc(arg1)
>>            1
>> Error: CHARACTER argument 'arg1' to 'c_loc' at (1) must have a length of 1
>>
>> I'm not sure where this restriction comes from (it's not part of the standard).
>>     
>
> One could argue that one should allow it as vendor extension, however, the
> restriction is in the standard:
>
> "Interoperability of intrinsic types"
> "A Fortran intrinsic type with particular type parameter values is
> interoperable with a C type if the type and kind type parameter value are
> listed in the table on the same row as that C type; if the type is character,
> interoperability also requires that the length type parameter be omitted or be
> specified by an initialization expression whose value is one."
>
> For interoperability one thus should use instead of
>   character(len=2) :: str
> rather
>   character(len=1,kind=c_char) :: str(2)
>
> (This is also the case for dummy arguments of BIND(C) procedures, though there
> one can as pass "a string" as actual argument, cf. "storage sequence".)
>
>
>   

Comment 3 kargl 2008-07-08 18:27:55 UTC
(In reply to comment #2)
> Subject: Re:  C_LOC and characters greater then one in
>  length.
> 
> My mistake, I did not see that in the standard.
> 
> It still would be nice however to have the non-standard feature for 
> characters of LEN > 1.
> 

It also may be appropriate to report this bug to the vendors you cited
as proof that gfortran had a bug.
Comment 4 Scot Breitenfeld 2008-07-08 18:40:35 UTC
Subject: Re:  C_LOC and characters greater then one in
 length.

I reported it to intel , their flag (-e03) used for checking for 
non-standard Fortran 2003 features missed it.
pgi compiler with -Mstandard also missed it.
g95 does not check for non-standard Fortran.

I guess the conclusion is that it is not a bug in gfortran.

kargl at gcc dot gnu dot org wrote:
> ------- Comment #3 from kargl at gcc dot gnu dot org  2008-07-08 18:27 -------
> (In reply to comment #2)
>   
>> Subject: Re:  C_LOC and characters greater then one in
>>  length.
>>
>> My mistake, I did not see that in the standard.
>>
>> It still would be nice however to have the non-standard feature for 
>> characters of LEN > 1.
>>
>>     
>
> It also may be appropriate to report this bug to the vendors you cited
> as proof that gfortran had a bug.
>
>
>   

Comment 5 Scot Breitenfeld 2008-07-09 14:24:58 UTC
The behavior conforms to the standard:

For the C_LOC(X) function X must be interpretable and interpretable for a character the standard says:

15.2.1 Interoperability of intrinsic types
  A Fortran intrinsic
4 type with particular type parameter values is interoperable with a C type if the type and kind type
5 parameter value are listed in the table on the same row as that C type; if the type is character, inter
6 operability also requires that the length type parameter be omitted or be specified by an initialization
7 expression whose value is one.

so it conforms to the standard by requiring

CHARACTER(LEN=1, KIND=C_CHAR), TARGET :: chr(2)
or
CHARACTER(KIND=C_CHAR), TARGET :: chr(2)