Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28542 Discussions

calling a c function from fortran with string argument

JacobB
Beginner
561 Views

Hello 

I am trying to call a c function in static library from fortran

on the "c" side the function name is 

void set_name(char *s, int *n)

 

on the fortran side I have created an interface as follows 

 

module f_c_interface
use ISO_C_Binding
interface
subroutine set_name(s,n) bind(C,name="set_name")
character (len=*), intent(inout) :: s
integer , intent(in) :: n
end subroutine set_name

end interface
end module f_c_interface

 

in the fortran program I have 

character (len=128) cname

cname = 'SomeName'

call set_name(cname,len_trim(cname))

 

on the c side I can see that the length is correct but cname does not pass correctly

 

Any help would really be appreciated 

best

jac

 

0 Kudos
1 Reply
Arjen_Markus
Honored Contributor I
554 Views

You should use a different declaration for the cname (s) argument in the interface:

character(len=1), intent(inout) :: s(*)

 With len=* you instruct the compiler to pass on the length as a hidden argument, one that a Fortran routine would know how to handle but would probably confuse a C function.

0 Kudos
Reply