From: CRDGW2::CRDGW2::MRGATE::"SMTP::CRVAX.SRI.COM::RELAY-INFO-VAX" 20-APR-1989 21:01 To: MRGATE::"ARISIA::EVERHART" Subj: Mixing C and Pascal params Message-Id: <8904210054.AA27402@crdgw1.ge.com> Received: From KL.SRI.COM by CRVAX.SRI.COM with TCP; Thu, 20 APR 89 17:28:33 PDT Received: from EAGLE.WESLEYAN.EDU by KL.SRI.COM with TCP; Thu, 20 Apr 89 17:09:06 PDT Date: 20-APR-1989 20:09:21.00 From: RSILVERMAN@EAGLE.WESLEYAN.EDU Subject: Mixing C and Pascal params To: INFO-VAX@EAGLE.WESLEYAN.EDU Bill Smith writes: > I have a problem. I am trying to call a pascal subroutine from C and > pass a char string. The following is the C program: > >main() >{ > char test[80] = "abcdefghijklmnop\0"; > cls(); > printf("%s \n",test); > index(test); > printf("%s \n",test); >} > Below is the Pascal Module I am linking in with the C program: > >MODULE CLEAR(OUTPUT); > [GLOBAL] PROCEDURE CLS; > BEGIN > WRITELN(CHR(27),'[H',CHR(27),'[J'); > END; > [GLOBAL] PROCEDURE INDEX(i : varying[A] of char); > BEGIN > WRITELN(I); > END; >END. (and the program crashes...) Bill, The problem is that Pascal expects the argument to be passed in a different way than that in which C is providing it. The code generated by the Pascal compiler for your routine 'INDEX' expects to find in the argument list a pointer to a string descriptor, which in turn points to a 'varying of char' record type. Instead, you provide just the address of the beginning of the character string. When the Pascal runtime tries to read the 'descriptor' at that address, it finds garbage instead, interprets it as an address, and you get your access violation. Here is a version of the calling C routine that works: #include descrip $DESCRIPTOR( test, "\5\0Hello" ); main() { cls(); index(&test); } Notice that the varying of char type contains a 16-bit count of the current length of the string, followed by the string characters themselves; hence the '\0\5' == short 5 in the string. VAX Pascal is a nightmare when it comes to this sort of thing; I much prefer the simple, straightforward conventions in C. Richard Silverman arpa: rsilverman@eagle.wesleyan.edu Computing Center bitnet: rsilverman@wesleyan Wesleyan University CIS: [72727,453] Middletown, CT 06457