HP C Programmer's Guide (92434-90009)

Chapter 3 47
Calling Other Languages
Comparing HP C and HP Pascal
2 bytes, if between 257 and 65536 elements.
4 bytes, otherwise.
If the default enumeration specifier is modified with a char or short type specifier, 1 or
2 bytes of storage are allocated. See Table 3-1 for a description of the sized enumerated
types.
This is important if the items are packed. For example, a 25-element enumeration in
HP Pascal can use 1 byte and be on a byte boundary, so you must use the HP C type
char or a sized enum declaration char enum.
3. HP C always indexes arrays from zero, while HP Pascal arrays can have lower bounds
of any user-defined scalar value. This is only important when passing an array using an
index to subscript the array. When passing the subscript between HP C and HP Pascal,
you must adjust the subscript accordingly. HP C always passes a pointer to the first
element of an array. To pass an array by value, enclose the array in a struct and pass
the struct.
4. HP C char arrays are packed one character per byte, as are HP Pascal arrays (even if
PACKED is not used). HP Pascal permits certain string operations with a packed array
of char when the lower bound is one.
5. The HP Pascal type STRING [
n
] uses a string descriptor that consists of the following: a
word containing the current length of the string,
n
bytes for the characters, and an
extra byte allocated by the HP Pascal compiler. Thus, the HP Pascal type STRING[10]
corresponds to the following HP C structure:
typedef struct {
int cur_len; /* 4 bytes */
char chars [10]; /* 10 bytes */
char extra_byte; /* 1 byte */
} STRING_10;
which is initialized like this:
STRING_10 this_string = {
0, /* The current length */
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* The 10 bytes */
0 /* The null byte */
};
Both the C structure and the Pascal string are 4-byte aligned.
6. HP Pascal also has a generic string type in which the maximum length is unknown at
compile time. Objects of this type have the same structure as in Note 5 above; the
objects are only used as VAR formal parameters.
7. A variable of this type is a pointer to a character array if the string is null-terminated;
HP Pascal will not handle the null byte in any special way. An HP C parameter of type
"pointer to char" corresponds to an HP Pascal VAR parameter of type "packed array of
char." However, the type definition of that VAR parameter must have the bounds
specified.
8. The size is equal to the size of all members plus any padding needed for the alignment.
(See Chapter 2 for details on alignment.) The alignment is that of the member with the
strictest alignment requirement.