I have a Fortran subroutine that calls a C function. One of the arguments to the C function is a fortran string. This is initialized as an empty string of length 512 bytes, and is passed to the C function as trim(str)//char(0)
, i.e. it is trimmed and a \0
is appended so that C sees its strlen
as 0.
character(len=512) :: str = ""
call C_foo ( trim(str)//char(0) )
Now inside the C function, can I do this
strncpy (str, "something", strlen("something") )
What I want to know is that since trim
is a transformational function as mentioned in its docs , does passing it to C actually alter the space allocated to it? Inside the C function, will a strncpy ever fail (as far as length of source string < 512) ? I tried it and it works fine, but I want to be sure it is safe.