Records and structures – HP SunSoft Pascal 4.0 User Manual

Page 129

Advertising
background image

The C–Pascal Interface

105

6

Records and Structures

In most cases, a Pascal record describes the same objects as its C structure
equivalent, provided that the components have compatible types and are
declared in the same order. The compatibility of the types depends mostly on
size and alignment. For more information on size and alignments of simple
components, see “Compatibility of Types for C and Pascal” on page 90.

By default, the alignment of a record is always four bytes and the size of a
record is always a multiple of four bytes. However, when you use

-calign

in

compiling the Pascal code, the size and alignment of the Pascal record matches
the size and alignment of the equivalent C structure.

A Pascal record of an integer and a character string matches a C structure of
the same constructs, as follows:

The commands to compile and
execute

ChrCAVar.p

and

ChrCAVarMain.c

hostname% pc -c -calign ChrCAVar.p

hostname% cc ChrCAVar.o ChrCAVarMain.c -lpc

hostname% a.out

This is a string

The Pascal procedure,

StruChr.p

. It is safer for the

Pascal procedure to explicitly
provide the null byte and include
it in the count before the string is
passed to C.

type

TLenStr = record

nbytes: integer;

chrstr: array [0..24] of char

end;

procedure StruChr(var v: TLenStr);

begin

v.NBytes := 14;

v.ChrStr := 'St. Petersburg' + chr(0);

end; { StruChr }

Advertising