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
The last parameter of
Format()
is anarray of const
(essentially, an array ofTVarRec
). A typedPChar
pointer is stored in theTVarRec.VPChar
field (TVarRec.VType=vtPChar
), whereas untyped pointers are stored in theTVarRec.VPointer
field (TVarRec.VType=vtPointer
). The%p
specifier is supported only for theVPointer
field. Only the%s
specifier is supported for theVPChar
field.So, you will have to type-cast
PChar
toPointer
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