title: Fortran to C , effect of trim on space allocated to string
body: <p>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 <code>trim(str)//char(0)</code> , i.e. it is trimmed and a <code>\0</code> is appended so that C sees its <code>strlen</code> as 0.</p>
  
  <pre><code>character(len=512)  :: str = ""
  call C_foo (  trim(str)//char(0)  )
  </code></pre>
  
  <p>Now inside the C function, 
  can I do this</p>
  
  <pre><code>strncpy (str, "something", strlen("something") )
  </code></pre>
  
  <p>What I want to know is that since <code>trim</code> is a transformational function as mentioned in its <a href="http://gcc.gnu.org/onlinedocs/gfortran/TRIM.html" rel="nofollow" title="docs">docs</a> , 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 &lt; 512) ? I tried it and it works fine, but I want to be sure it is safe.</p>