12.07.2015 Views

LF95 Linux User's Guide - Lahey Computer Systems

LF95 Linux User's Guide - Lahey Computer Systems

LF95 Linux User's Guide - Lahey Computer Systems

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 3 Mixed Language Programmingfunction MYFUNC(str1, str2)character(len=*) :: str1, str2, myfuncmyfunc = str1//str2//char(0)returnendWhen called by the following C program,void myfunc_(char *str1, int i, char *str2, \char *str3, int j, int k);MAIN__(){/* Leave space for NULL in character declarations */char res[10], ch[4], msg[7];strcpy(ch, "Hi ");strcpy(msg, "there!");myfunc_(res, 10, ch, msg, 3, 6);printf("Result received by C: %s\n", res);}The following output is generated:Result received by C: Hi there!In the call to MYFUNC from C, the first and second arguments are the value and length, respectively,of the result returned by MYFUNC. The last two arguments are the respective lengthsof the character arguments being passed to MYFUNC.Passing Data through Common BlocksThe variables in a Fortran common block may be referenced as C structure members.Example: Named CommonIn the following Fortran program, the variables in common block “ext”common /ext/ i, ji = 1j = 2call sub()endare accessed by a C function as follows:extern struct tab {int i, j;} ext_;void sub_(){printf("i=%i j=%i\n", ext_.i, ext_.j);}62 <strong>Lahey</strong>/Fujitsu <strong>Linux</strong>64 Fortran User’s <strong>Guide</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!