I get error#6360 when I attempted to move this code from IVF10* to the eval edition of IVF11*
defined in a structure as so:
character (kind=1),dimension(1:256) :: jobname
write(6,*) ' Jobname = ',trim(adjustl(this%jobname))
链接已复制
7 回复数
And that's what should be expected.
vname is an array whose elements are strings of length = 1, and the function trim takes a scalar argument. Depending on what you want to do, declare vname to be a string of length 4, or apply trim and adjustl to an element of vname rather than to the array.
vname is an array whose elements are strings of length = 1, and the function trim takes a scalar argument. Depending on what you want to do, declare vname to be a string of length 4, or apply trim and adjustl to an element of vname rather than to the array.
I'm in a catch22. I'm calling a fortran routine from C.
If it is defined as so:
subroutine fortranRoutine(charvar) bind(C,name='fortranRoutine')
character(len=256) ,intent(in)::charvar
results in error #8532, character dummy argument with length other than 1 is not interoperable
However if I declare
character(len=1),intent(in)::charvar(1:256)
I'm not able to use charvar with trim(adjustl(charvar)) without producing the #6360 error.
I need something similar to the C "cast" to redefine the shape of charvar I think.
There are a couple of different ways to deal with it. TRANSFER is one, a combination of C_LOC and C_F_POINTER is another.
You're right that this is a hole in the C interoperability feature. While you can pass a CHARACTER(n) variable out to C with an interface declared as an array of CHARACTER(1), there's no corresponding way to "receive" such a thing.
You're right that this is a hole in the C interoperability feature. While you can pass a CHARACTER(n) variable out to C with an interface declared as an array of CHARACTER(1), there's no corresponding way to "receive" such a thing.
Ok, this compiles.
subroutine fortranRoutine(charvar) bind(C,name=fortranRoutine)
character(len=1),intent(in)::charver(1:256)
character(len=256)::scalar256,x
x=transfer(charvar,scalar256)
any other gotcha's? Is there a cleaner way?
subroutine fortranRoutine(charvar) bind(C,name=fortranRoutine)
character(len=1),intent(in)::charver(1:256)
character(len=256)::scalar256,x
x=transfer(charvar,scalar256)
any other gotcha's? Is there a cleaner way?
[bash]module interop_test contains subroutine fortranRoutine(charvar) bind(C,name='fortranRoutine') use ISO_C_BINDING implicit none character(kind=C_CHAR),target :: charvar(256) character(kind=C_CHAR),pointer :: charvarString*(256) type(C_PTR) charvarAddress charvarAddress = C_LOC(charvar) call C_F_POINTER(charvarAddress,charvarString) write(*,'(a)') trim(charvarString) end subroutine fortranRoutine end module interop_test program interop_string use interop_test implicit none call fortranRoutine([character(256) :: 'Hello, world']) end program interop_string [/bash]
