HP SunSoft Pascal 4.0 User Manual

Page 152

Advertising
background image

128

Pascal 4.0 User’s Guide

6

See this example:

The C function,

NonPas.c

. In

the function

for_C

,

s

is a

pointer (declared

var

in the

procedure declaration), and

len

is not a pointer (not declared

var

in the procedure

declaration). In the function

for_nonpascal

,

s

is still a

pointer (though not declared

var

in the procedure

declaration), and

len

is now a

pointer (though not declared

var

).

#include <stdio.h>

void for_C(char *s, int len)

{

int i;

for (i = 0; i < len; i++)

putchar(s[i]);

putchar('\n');

}

void for_NonPascal(char *s, int *len)

{

int i;

for (i = 0; i < *len; i++)

putchar(s[i]);

putchar('\n');

}

The Pascal main program,

NonPasMain.p

program NonPasMain;

var

s: string;

procedure for_C(var s: string; len: integer); external c;

procedure for_NonPascal(var s: string; len: integer); nonpascal;

begin

s :='Hello from Pascal';

for_C(s, 18);

for_NonPascal(s, 18);

end. { NonPasMain }

The commands to compile and
execute

NonPas.c

and

NonPasMain.p

hostname% cc -c NonPas.c

hostname% pc NonPas.o NonPasMain.p

hostname% a.out

Hello from Pascal

Hello from Pascal

Advertising