Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29233 Discussions

Crashed when modify string in Fortran for C calls Fortran

wu_internet
Beginner
409 Views
I am not sure if anyone has posted this topic already, As I searched and have not been able to find, I post my question here.
I need to use the C to call Fortran to get a character string data, what I prefer is the string use the C type string and pass
by reference, so that I do not have to use the additional argument about the string length. What I did is:
Fortran:
=======
SUBROUTINE GETSTRING(ASTRING)
!DEC$ ATTRIBUTES C, ALIAS '_GETSTRING' :: GETSTRING
CHARACTER(132) ASTRING
!DEC$ ATTRIBUTES REFERENCE :: ASTRING
!Assignment data to ASTRNG
ASTRING(1:1) = 'A'
!AT THE END OF THE STRING, WILL ADD CHAR(0) TO ASTRING
RETURN
END

C PART CODE
===========
void GETSTRING(char *string);
//The following is the calling statement
char* astring = "Initial value of astring";
GETSTRING(string);

.....
Result
======
When C called Fortran, the initial value of astring is passed into the Fortran correctly, when
Fortran tried to modify the ASTRING (THE STATEMENT TO SET THE FIRST CHARACTER TO BE 'A'), the
program crashed, and says "access violation".
any experience on using this will be appreciated.
David.
0 Kudos
2 Replies
TimP
Honored Contributor III
409 Views
You must provide the additional length argument on the C side, to support Fortran CHARACTER type, even if you don't use it. If you want to use non-standard code, you could declare an array of single byte integers on the Fortran side, thus avoiding use of the length argument.
0 Kudos
wu_internet
Beginner
409 Views
Thanks for the message. I am not sure if your reply conflicts with the statement provided in the user guide, In thetable named"Effect of ATTRIBUTES Options on Character Strings Passed as Arguments" (MIXED Programming Part),
It states that when "String with REFERENCE option", the call will be
"Passed by reference, no length " therefore, I take itthatthere should beno length needed.

Regards,

David

0 Kudos
Reply