This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug fortran/36771] Non-Standard addition for C_LOC and character strings
- From: "brtnfld at hdfgroup dot org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 14 Jul 2008 20:45:42 -0000
- Subject: [Bug fortran/36771] Non-Standard addition for C_LOC and character strings
- References: <bug-36771-16432@http.gcc.gnu.org/bugzilla/>
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
------- Comment #1 from brtnfld at hdfgroup dot org 2008-07-14 20:45 -------
For the simple program,
PROGRAM main
USE ISO_C_BINDING
CHARACTER(LEN=2, KIND=C_CHAR), TARGET :: chr
TYPE(C_PTR) :: chr_ptr
chr_ptr = C_LOC(chr)
END PROGRAM main
the work around is to:
CHARACTER(LEN=1, KIND=C_CHAR), dimension(1:2), TARGET :: chr
However, forcing it to be an array instead of a character(LEN>1) causes
problems with generic interfaces. Take for example the following code which to
me looks to be standard compliant, via the standard the CHARACTER(LEN=4) actual
argument is allowed to match a CHARACTER(LEN=1,KIND=C_CHAR), DIMENSION(4) dummy
argument (12.4.1.2).
But the compiler will not recognize subroutine 'Two' as a valid interface for
the call indicated since the dimensions check is not satisfied even though it
is a valid interface. So if you are going by the standard you can not use a
generic interface to handle a scalar character (len>1) and an array of
characters (keeping in mind that all the subroutines have to have LEN=1 so that
you can use C_LOC).
MODULE modtest
USE ISO_C_BINDING
INTERFACE One
MODULE PROCEDURE Two
END INTERFACE
CONTAINS
SUBROUTINE Two( chr )
CHARACTER(LEN=1, KIND=C_CHAR), DIMENSION(1:4) :: chr
END SUBROUTINE Two
END MODULE modtest
PROGRAM main
USE ISO_C_BINDING
USE modtest
CHARACTER(LEN=4, KIND=C_CHAR) :: chrScalar
chrScalar = 'Scal'
! This does not work, it will not find the interface.
CALL One( chrScalar )
! This works
CALL Two( chrScalar )
END PROGRAM main
It is simply not feasible to cast all character strings into character arrays.
This problem would all go away if we did not have to recast the string as an
array.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36771