6

The commands to compile and execute GloVar.p and GloVarMain.c without –xl. With -xl, the Pascal integer must be paired with a C short int and declared public since the default visibility is private.

hostname% pc -c GloVar.p hostname% cc GloVar.o GloVarMain.c

hostname% a.out 2001

File-Passing Between Pascal and C

You can pass a file pointer from Pascal to C, then have C do the I/O, as in:

The C procedure,

#include <stdio.h>

UseFilePtr.c

 

 

void UseFilePtr (FILE *ptr)

 

{

 

{ /* Write to the file: */

 

fprintf( ptr, "[1] Passing the file descriptor \n") ;

 

fprintf( ptr, "[2] and writing information \n") ;

 

fprintf( ptr, "[3] to a file \n") ;

 

}

 

 

The Pascal main program, UseFilePtrMain.p

program UseFilePtrMain; var

f: text;

cfile: univ_ptr;

procedure UseFilePtr(cf: univ_ptr); external c;

begin

rewrite(f, 'myfile.data');

{ Make the file. }

cfile := getfile(f);

{ Get a file pointer. }

UseFilePtr(cfile);

{ Call the C function. }

end. { UseFilePtrMain }

 

134

Pascal 4.0 User’s Guide