有如下C语言的结构加联合嵌套的定义:
struct in_addr {
union {
struct { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b;
struct { u_short s_w1,s_w2; } S_un_w;
u_long S_addr;
} S_un;
};
请问各位高手用PASCAL语言如何定义?在PASCAL中有和C的联合相同的数据类型嘛?
struct in_addr {
union {
struct { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b;
struct { u_short s_w1,s_w2; } S_un_w;
u_long S_addr;
} S_un;
};
请问各位高手用PASCAL语言如何定义?在PASCAL中有和C的联合相同的数据类型嘛?
TShapeList = (Rectangle, Triangle, Circle, Ellipse, Other);
TFigure = record
case TShapeList of
Rectangle: (Height, Width: Real); Triangle: (Side1, Side2, Angle: Real);
Circle: (Radius: Real);
Ellipse, Other: ();
end;
Variant parts in recordsA record type can have a variant part, which looks like a case statement. The variant
part must follow the other fields in the record declaration.
To declare a record type with a variant part, use the following syntax.ഊDat a t yp es, variables, a nd con stants 5-23
Struc t u re d t y p e s
type recordTypeName =record
fieldList 1 :type 1 ;
ƒ
fieldList n :type n ;
case tag ordinalType of
constantList 1 :(variant 1 );
ƒ
constantList n :(variant n );
end;
The first part of the declaration—up to the reserved word case—is the same as that of
a standard record type. The remainder of the declaration—from case to the optional
final semicolon—is called the variant part. In the variant part,
• tag is optional and can be any valid identifier. If you omit tag, omit the colon (:)
after it as well.
• ordinalType denotes an ordinal type.
•Each constantList is a constant denoting a value of type ordinalType, or a comma-delimited
list of such constants. No value can be represented more than once in the
combined constantLists.
•Each variant is a comma-delimited list of declarations resembling the fieldList type
constructions in the main part of the record type. That is, a variant has the form
fieldList 1 :type 1 ;
ƒ
fieldList n :type n ;
where each fieldList is a valid identifier or comma-delimited list of identifiers, each
type denotes a type, and the final semicolon is optional. The types must not be long
strings, dynamic arrays, variants (that is, Variant types), or interfaces, nor can they
be structured types that contain long strings, dynamic arrays, variants, or
interfaces; but they can be pointers to these types.
Records with variant parts are complicated syntactically but deceptively simple
semantically. The variant part of a record contains several variants which share the
same space in memory. You can read or write to any field of any variant at any time;
but if you write to a field in one variant and then to a field in another variant, you may
be overwriting your own data. The tag, if there is one, functions as an extra field (of
type ordinalType) in the non-variant part of the record.
Variant parts have two purposes. First, suppose you want to create a record type that
has fields for different kinds of data, but you know that you will never need to use all
of the fields in a single record instance. For example,
type
TEmployee =ecord
FirstName,LastName:string [40 ];
BirthDate:TDate;
case Salaried:Boolean of
True:(AnnualSalary:Currency);
False:(HourlyWage:Currency);
end ;ഊS t ruc t u re d t y p e s
The idea here is that every employee has either a salary or an hourly wage, but not
both. So when you create an instance of TEmployee, there is no reason to allocate
enough memory for both fields. In this case, the only difference between the variants
is in the field names, but the fields could just as easily have been of different types.
Consider some more complicated examples:
type
TPerson =record
FirstName,LastName:string [40 ];
BirthDate:TDate;
case Citizen:Boolean of
True:(Birthplace:string [40 ]);
False:(Country:string [20 ];
EntryPort:string [20 ];
EntryDate,ExitDate:TDate);
end ;
type
TShapeList =(Rectangle,Triangle,Circle,Ellipse,Other);
TFigure =record
case TShapeList of
Rectangle:(Height,Width:Real);
Triangle:(Side1,Side2,Angle:Real);
Circle:(Radius:Real);
Ellipse,Other:();
end ;
For each record instance, the compiler allocates enough memory to hold all the fields
in the largest variant. The optional tag and the constantLists (like Rectangle, Triangle,
and so forth in the last example above) play no role in the way the compiler manages
the fields; they are there only for the convenience of the programmer.
The second reason for variant parts is that they let you treat the same data as
belonging to different types, even in cases where the compiler would not allow a
typecast. For example, if you have a 64-bit Real as the first field in one variant and a
32-bit Integer as the first field in another, you can assign a value to the Real field and
then read back the first 32 bits of it as the value of the Integer field (passing it, say, to a
function that requires integer parameters).
union {
struct { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b;
struct { u_short s_w1,s_w2; } S_un_w;
u_long S_addr;
} S_un;
};
我按照您的指点翻译如下:
TIpAndPortList = (IP, Port, Other);
TS_un_b = record
s_b1,s_b2,s_b3,s_b4: AnsiChar;
end; TS_un_w = record
s_w1,s_w2: Word;
end; Pin_addr = ^Tin_addr;
Tin_addr = record
case TIpAndPortList of
IP: (S_un_b: TS_un_b);
Port: (S_un_w: TS_un_w);
Other: (S_addr: Longword);
end;
这样的定义在DELPHI中编译通过,但调用他的动态连接库中的函数后出现错误,不知道是何故呢?
{$EXTERNALSYM SunB}
SunB = packed record
s_b1, s_b2, s_b3, s_b4: u_char;
end; {$EXTERNALSYM SunW}
SunW = packed record
s_w1, s_w2: u_short;
end; PInAddr = ^TInAddr;
{$EXTERNALSYM in_addr}
in_addr = record
case integer of
0: (S_un_b: SunB);
1: (S_un_w: SunW);
2: (S_addr: u_long);
end;
TInAddr = in_addr; PSockAddrIn = ^TSockAddrIn;
{$EXTERNALSYM sockaddr_in}
sockaddr_in = record
case Integer of
0: (sin_family: u_short;
sin_port: u_short;
sin_addr: TInAddr;
sin_zero: array[0..7] of Char);
1: (sa_family: u_short;
sa_data: array[0..13] of Char)
end;
TSockAddrIn = sockaddr_in;