FORTRAN and C Character String and Integer Array

ID: Q63308


The information in this article applies to:
  • Microsoft FORTRAN for MS-DOS, versions 5.0, 5.1
  • Microsoft FORTRAN for OS/2, versions 5.0, 5.1


SUMMARY

The following code is designed to show passing arrays and character strings from FORTRAN to C and from C to FORTRAN.


MORE INFORMATION

There are two programs here. The first has a FORTRAN main and C subroutine; the second a C main and FORTRAN subroutine. Each program does essentially the same thing -- passes an array of 5 INTEGER*4 and a character string to the subroutine, which then prints the values.

C version 6.0 and FORTRAN version 5.0 were used in this example. FORTRAN needs to be built with C compatible libraries, and the modules need to be linked with the /NOE switch. Each module needs to be compiled with the same floating-point and memory-module options (for example, /AL /FPi).

The following is the FORTRAN main:

Sample Code (.for)


C Note that the passed string has a zero length C style string
C appended so that it appends a NULL terminating character.

      INTERFACE TO SUBROUTINE CPRINT [C] (I,STRING)
      INTEGER*4 I [REFERENCE]
      CHARACTER*(*) STRING [REFERENCE]
      END

      PROGRAM FMAIN

      INTEGER*4 I(5)
      CHARACTER*255 PRINTSTRING /'This example shows passing an array an
     +d a string.'/ 

      I(1) = 5
      I(2) = 4
      I(3) = 3
      I(4) = 2
      I(5) = 1


      CALL CPRINT(I,PRINTSTRING//''C)

      END 
The following is the start of the C subroutine:

Sample Code (.c)


#include <stdio.h>

void cprint(long int i[], char * printstring)
{
  int count;

  printf("\n");
  for (count = 0; count < 5;count++)
    printf("\t%li",i[count]);
  printf("\n");

  printf("%s",printstring);
}

The following is the start of the C main program: 

Sample Code (.c)


extern void fortran fprint (long int *, char *);

main ()
{
    long int i[5];
    char printstring[255] = "This example shows passing an array and"
                            " a string.";

    i[0] = 5;
    i[1] = 4;
    i[2] = 3;
    i[3] = 2;
    i[4] = 1;

    fprint(i,printstring);
} 
The following is the start of the FORTRAN subroutine:

Sample Code (.for)


C The declaration of PRINTSTRING has to be equal to or less than the
C size of the string passed from C. If it is not, other data will get
C accessed, possibly causing a protection violation under OS/2.

      SUBROUTINE FPRINT (I,PRINTSTRING)
      INTEGER*4 I(*)
      CHARACTER*255 PRINTSTRING

      INTEGER  J

C The following line truncates the string at the end of the C string.
C (The C string end-of-string character is CHAR(0).)
      PRINTSTRING = PRINTSTRING(1:INDEX(PRINTSTRING,CHAR(0)))

      WRITE (*,*) (I(J),J=1,5)
      WRITE (*,*)
C The substring (1:lentrim()) is printed instead of just the
C variable because this way only the characters stored in the
C variable are printed, and not the entire length of the variable
C padded with spaces (255 spaces).
      WRITE (*,*) PRINTSTRING(1:LEN_TRIM(PRINTSTRING))
      END 

Additional query words: nofps kbinf 5.00 5.10 mixed

Keywords :
Version : :5.0,5.1
Platform :
Issue type :


Last Reviewed: November 1, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.