title: Returning character string of unknown length in fortran
body: <p>Apologies for the rather simple question, I just can't seem to find ANY good fortran docs.</p>
  
  <p>I'm trying to write a function that reads from a unit, trims the output and appends the null terminator, something like:</p>
  
  <pre><code>character(*) function readCString()
    character*512 str
    read(1, *) str
    readCString = TRIM(str)//char(0)
    return
  end function readCString
  </code></pre>
  
  <p>However, I KNOW this doesn't work yet it compiles. Segmentation faults have not been my friend recently. Without the "character(*)" before the function keyword it will not compile, and with any value in place of the star it also breaks, most likely because:</p>
  
  <pre><code>TRIM(str)//char(0)
  </code></pre>
  
  <p>is not the same length as the number I put in place of the star.
  I'm very new to fortran but am trying to interface some fortran code with C (hence the null terminator).</p>
  
  <p>Thanks for any help.</p>