Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.

Storing C Strings

DavidWhite
Valued Contributor II
762 Views
How can I store a C-string into a Fortran variable? For a string constant, I believe I can declare it as 'qwerty'C, e.g. when passing to a C routine.
To save this in a Fortran variable,would I need to use 'qwerty'//CHAR(0)?
0 Kudos
2 Replies
Jugoslav_Dujic
Valued Contributor II
762 Views
Yes.

In general, you should use TRIM to ensure the text is trimmed correctly, like:
CString = TRIM(FString)//CHAR(0)
. To perform conversion other way round, use:
iLen = INDEX(CString, CHAR(0))
IF (iLen.GT.0) FString = CString(1:iLen-1)
Jugoslav
0 Kudos
jim_dempsey
Beginner
762 Views
Jugoslav,
A few problems with the code example
CString = TRIM(FString)//CHAR(0)
Assumes CString is larger than the trimmed FString.
This would result in CString being absent of a null.
A test should be done to assure that CString is large
enough and to take appropriate action if it is not.
iLen = INDEX(CString, CHAR(0))
IF (iLen.GT.0) FString = CString(1:iLen-1)
if a Null is not in the CString FString receives no data.
Also if null string is passed (null at 1) you have a subscript error
Use:
IF (iLen.GT.1) then
  FString = CString(1:iLen-1)
else
  FString = ' '
endif
Jim Dempsey
0 Kudos
Reply