I am now trying to pass a string as well as an integer between C and Pascal. I am getting a segmentation fault. I would be grateful if tell me what I have done wrong. I attach the files pcallc.p, ccall.c ccall.h and Makefile and show the output at the end.
Many thanks, Chris
pcallc.p: [ program pascalcallsC(input,output);
procedure world; external name 'world'; procedure w1(var i : integer; var s : cstring); external name 'c_w1';
var i : integer; s : cstring; begin writeln("Hello world from Pascal"); world; writeln("Returned from C to Pascal\n"); i := 96; w1(i,s); writeln("Returned from C again I = ",i); writeln("Return from C cstring = ",s); {segmentation fault on this line} s := "This is a CString"; writeln("Trying out pascal ",s); end. ]
ccall.c: [ #include <stdio.h> #include <string.h> #include "ccall.h"
void world (void) { printf("\nHello World from C\n"); }
void c_w1 (int *i, char *s) { printf("Now in function c_w1\n"); strcpy(s,"Mary had a little lamb\n"); printf("Hello again from C i = %i\n%sXXX\n",(*i),s); (*i) = 56; printf("Leaving C\n"); } ]
ccall.h: [ extern void world(void); extern void w1(int *i, char *s); ]
Makefile: [ pcallc : pcallc.p ccall.o gpc -o pcallc pcallc.p ccall.o --cstrings-as-strings ccall.o : ccall.c gcc -c ccall.c clean: rm *.o pcallc ccall.o ]
output: [nmansys@mmme1 pcallsc2]$ pcallc Hello world from Pascal
Hello World from C Returned from C to Pascal
Now in function c_w1 Hello again from C i = 96 Mary had a little lamb XXX Leaving C Returned from C again I = 56 Segmentation fault
Dr Christian Hicks Senior Lecturer, School of Mechanical & Systems Engineering, Stephenson Building, University of Newcastle upon Tyne, NE1 7RU. Phone: +44 191 222 6238 Mobile 0795 8317804 Fax: + 44 191 222 8600 Homepage: http://www.staff.ncl.ac.uk/chris.hicks