I have the following program.
var a: PChar := PChar('abc');
ShowMessage(Format('%p - %p', [@a, a]));
When I run it, I got this error:
Project Project1.exe raised exception class EConvertError with message 'Format '%p - %p' invalid or incompatible with argument'.
The problem is that a
can't be formatted as %p
. But as I understand, PChar is defined as ^Char
. So a PChar is essentially a pointer to Char. It's a pointer. Why can't format it as %p
? The following code works without any issue:
var c: Char := 'x';
var a := @c;
Format('%p - %p', [@a, a]); // 0019F364 - 0019F36A
CodePudding user response:
The last parameter of Format()
is an array of const
(essentially, an array of TVarRec
). A typed PChar
pointer is stored in the TVarRec.VPChar
field (TVarRec.VType=vtPChar
), whereas untyped pointers are stored in the TVarRec.VPointer
field (TVarRec.VType=vtPointer
). The %p
specifier is supported only for the VPointer
field. Only the %s
specifier is supported for the VPChar
field.
So, you will have to type-cast PChar
to Pointer
if you want to use %p
.
I have filed a report with Embarcadero about this:
RSP-37775: Update SysUtils.Format() to allow %p for all types of pointers