Program gf
Use iso_c_binding
Real( c_double ), Dimension( 1:10 ), Target :: a
Call test( a )
Contains
Subroutine test( aa )
Real( c_double ), Dimension( : ), Target :: aa
Type( c_ptr ), Pointer :: b
b = c_loc( aa( 1 ) )
End Subroutine test
End Program gf
Wot now? nagfor -f2003 -c gf.f90
NAG Fortran Compiler Release 5.2(721)
[NAG Fortran Compiler normal termination]
Wot now? g95 -std=f2003 -c gf.f90
Wot now? ifort -stand=f03 gf.f90
Wot now? f90 gf.f90
Wot now? ~/Downloads/open64-4.2.4/bin/openf95 gf.f90
Wot now? gfortran -c -std=f2003 gf.f90
gf.f90:17.15:
b = c_loc( aa( 1 ) )
1
Error: Assumed-shape array 'aa' at (1) cannot be an argument to the
procedure 'c_loc' because it is not C interoperable
Wot now?
(f90 is the sun or whatever it is called now compiler)
So is gfortran correct? Version 4.5.2 if it is of interest,
Ian
(snip)
>> b = c_loc( aa( 1 ) )
>> 1
>> Error: Assumed-shape array 'aa' at (1) cannot be an argument to the
>> procedure 'c_loc' because it is not C interoperable
> Compiler bug - the argument to c_loc in your example program isn't an
> array, it is a scalar (and that scalar is interoperable and meets the
> other requirements for an argument to c_loc).
Does look like a compiler bug to me, too.
Note that in C, a pointer to an array is equivalent to a pointer
to its first element. (Or is it the other way around.)
That isn't true in Fortran, and the compiler shouldn't assume
that it is. (That is, assume that you meant c_loc(aa).)
If the actual argument is contiguous, then you should be able to
use the pointer in C to address the whole array. If it isn't
contiguous, then you can't do that.
Did you try a Fortran pointer pointing to a(1), and then as an
argument to c_loc()?
-- glen
Thanks, that's what I thought. I'll report it in a bit,
Ian
Yup, indeed. This is ultimately the reason I'm doing this.
>
> If the actual argument is contiguous, then you should be able to
> use the pointer in C to address the whole array. If it isn't
> contiguous, then you can't do that.
>
> Did you try a Fortran pointer pointing to a(1), and then as an
> argument to c_loc()?
>
Good idea! Why didn't I think of that ...
It works, ta muchly!
Ian
Thanks Tobias!
Ian