取消引用记录元素
De-Referencing Record Elements
我有一个记录正在被一个用 C 编写的具有以下结构的 DLL 填充:
Thackrf_device_list = record
serial_numbers: PPAnsiChar;
usb_board_ids: ^hackrf_usb_board_id;
usb_device_index: PInteger;
devicecount: Integer;
usb_devices: PPointer;
usb_devicecount: Integer;
end;
记录似乎填写正确,因为返回的整数 'devicecount' 和 'usb_devicecoun' 符合预期。
我想不通的是如何取消对其他元素的引用。
我需要的是食谱或一些好的例子。
任何帮助
解引用指针非常简单。指针属于记录、class 还是只是某个变量并不重要。您可以通过在指针标识符之后添加 ^
运算符来取消引用指针。
例如:
var
LDeviceList: Thackrf_device_list;
LSomePAnsiChar: PAnsiChar;
LSomeUsbBoardIDs: hackrf_usb_board_id;
LSomeInteger: Integer;
LSomePointer: Pointer;
begin
// fill LDeviceList by the C-written DLL
LSomePAnsiChar := LDeviceList.serial_numbers^; // returns a pointer to the first char of a string containing the serials as 'serial_numbers' seems to be a pointer to a pointer (PAnsiChar)
LSomeUsbBoardIDs := LDeviceList.usb_board_ids^; // gives you the record of type 'hackrf_usb_board_ids'
LSomeInteger := LDeviceList.usb_device_index^; // returns the device index as an integer value
LSomePointer := LDeviceList.usb_devices^; // returns a pointer as 'usb_devices' seems to be a pointer to a pointer
// ...
end;
取消引用 Delphi 中的指针是使用取消引用运算符的简单问题。当一个指针被解引用时,它会产生一个指针所引用类型的值。对于无类型指针,这意味着您需要进行类型转换以告诉编译器解引用产生的类型:
var
intPtr: PInteger;
charPtr: PChar;
ptr: Pointer;
int: Integer;
char: Char;
begin
// ..
int := intPtr^; // read as: int := the int pointed to by intPtr
char := charPtr^; // read as: char := the char pointed to by charPtr
// For untyped pointers you need to cast
int := Integer(ptr^);
char := Char(ptr^);
end;
因此,如果指针是指向指针的指针,那么取消引用它会产生该指针,然后可以取消引用该指针。
您可以将此解引用指针用于变量以及 类 的成员和记录(如您的示例中所示)。但是,编译器和语言也支持 自动 在适当的地方解除引用。
也就是说,如果您有一个指向某些记录类型的指针并希望引用 that 记录的特定成员,您仍然可以使用取消引用运算符 or 你可以省略解引用运算符(编译器会自动为你解引用)。
以简单的TPoint记录为例:
type
PPoint = ^TPoint;
TPoint = record
X, Y: Integer;
end;
var
pointPtr: PPoint;
myX, myY: Integer;
begin
//..
myX := pointPtr^.X;
myY := pointPtr^.Y;
// or you can simply write:
myX := pointPtr.X;
myY := pointPtr.Y;
end;
我有一个记录正在被一个用 C 编写的具有以下结构的 DLL 填充:
Thackrf_device_list = record
serial_numbers: PPAnsiChar;
usb_board_ids: ^hackrf_usb_board_id;
usb_device_index: PInteger;
devicecount: Integer;
usb_devices: PPointer;
usb_devicecount: Integer;
end;
记录似乎填写正确,因为返回的整数 'devicecount' 和 'usb_devicecoun' 符合预期。
我想不通的是如何取消对其他元素的引用。
我需要的是食谱或一些好的例子。
任何帮助
解引用指针非常简单。指针属于记录、class 还是只是某个变量并不重要。您可以通过在指针标识符之后添加 ^
运算符来取消引用指针。
例如:
var
LDeviceList: Thackrf_device_list;
LSomePAnsiChar: PAnsiChar;
LSomeUsbBoardIDs: hackrf_usb_board_id;
LSomeInteger: Integer;
LSomePointer: Pointer;
begin
// fill LDeviceList by the C-written DLL
LSomePAnsiChar := LDeviceList.serial_numbers^; // returns a pointer to the first char of a string containing the serials as 'serial_numbers' seems to be a pointer to a pointer (PAnsiChar)
LSomeUsbBoardIDs := LDeviceList.usb_board_ids^; // gives you the record of type 'hackrf_usb_board_ids'
LSomeInteger := LDeviceList.usb_device_index^; // returns the device index as an integer value
LSomePointer := LDeviceList.usb_devices^; // returns a pointer as 'usb_devices' seems to be a pointer to a pointer
// ...
end;
取消引用 Delphi 中的指针是使用取消引用运算符的简单问题。当一个指针被解引用时,它会产生一个指针所引用类型的值。对于无类型指针,这意味着您需要进行类型转换以告诉编译器解引用产生的类型:
var
intPtr: PInteger;
charPtr: PChar;
ptr: Pointer;
int: Integer;
char: Char;
begin
// ..
int := intPtr^; // read as: int := the int pointed to by intPtr
char := charPtr^; // read as: char := the char pointed to by charPtr
// For untyped pointers you need to cast
int := Integer(ptr^);
char := Char(ptr^);
end;
因此,如果指针是指向指针的指针,那么取消引用它会产生该指针,然后可以取消引用该指针。
您可以将此解引用指针用于变量以及 类 的成员和记录(如您的示例中所示)。但是,编译器和语言也支持 自动 在适当的地方解除引用。
也就是说,如果您有一个指向某些记录类型的指针并希望引用 that 记录的特定成员,您仍然可以使用取消引用运算符 or 你可以省略解引用运算符(编译器会自动为你解引用)。
以简单的TPoint记录为例:
type
PPoint = ^TPoint;
TPoint = record
X, Y: Integer;
end;
var
pointPtr: PPoint;
myX, myY: Integer;
begin
//..
myX := pointPtr^.X;
myY := pointPtr^.Y;
// or you can simply write:
myX := pointPtr.X;
myY := pointPtr.Y;
end;