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 typedPCharpointer is stored in theTVarRec.VPCharfield (TVarRec.VType=vtPChar), whereas untyped pointers are stored in theTVarRec.VPointerfield (TVarRec.VType=vtPointer). The%pspecifier is supported only for theVPointerfield. Only the%sspecifier is supported for theVPCharfield.So, you will have to type-cast
PChartoPointerif 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