- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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)?
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Yes.
In general, you should use TRIM to ensure the text is trimmed correctly, like:
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))Jugoslav
IF (iLen.GT.0) FString = CString(1:iLen-1)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page