HP C Programmer's Guide (92434-90009)

52 Chapter3
Calling Other Languages
Comparing HP C and HP Pascal
/* a Pascal routine */
extern int pass_char_arrays (/* pac10,
var pac10,
string_10,
var string[10] */);
main(void)
{
static struct string_10 a, b, c, d;
int ret_val;
strcpy (a.chars, "aaaaaaaaa");
strcpy (b.chars, "bbbbbbbbb");
strcpy (c.chars, "ccccccccc");
c.cur_len = strlen (c.chars);
strcpy (d.chars, "ddddddddd");
d.cur_len = 5;
ret_val = pass_char_arrays (a.chars, b.chars, &c, &d);
printf ("a: %s\n", a.chars); /* prints aaaaaaaaa */
printf ("b: %s\n", b.chars); /* prints yyyyyyyyy */
printf ("c: %s\n", c.chars); /* value parm prints xxxxxxxxx */
printf ("d: %s\n", d.chars); /* prints wwwwwdddd */
printf ("return mask: %d\n", ret_val); /* print 0 */
ret_val = pass_a_string (&c);
printf ("c: %s\n", c.chars); /* prints qqqqqqqqq */
printf ("return mask: %d\n", ret_val); /* print 0 */
return 0;
}
The program produces the following output:
a: aaaaaaaaa
b: yyyyyyyyy
c: xxxxxxxxx
d: wwwwwdddd
return mask: 0
c: qqqqqqqqq
return mask: 0
The routine pass_a_string() expects a generic string (described in Note 6 above), so
you must pass an extra argument. The extra argument consists of a value parameter
containing the maximum length of the char array.
3. HP Pascal routines do not maintain a null byte at the end of HP C strings. HP Pascal
determines the current length of the string by maintaining the length in a 4-byte
integer preceding the character data. When an HP Pascal procedure or function (that
takes as a parameter a string by reference) is called, the following code is necessary if
the Pascal routine modiļ¬es the string:
pass_a_string (a, temp); /* From note 2 above */
a.chars[a.cur_len] = '\0';