[fpc-pascal] Pointer question

Sven Barth pascaldragon at googlemail.com
Wed Aug 9 07:53:33 CEST 2023


Hairy Pixels via fpc-pascal <fpc-pascal at lists.freepascal.org> schrieb am
Mi., 9. Aug. 2023, 04:59:

> I just noticed this by accident and I don't understand. Why does " x :=
> @r; " compile? I would expect it to fail like the next line where I cast to
> the explicit type.
>
> type
>   TMyRec = record end;
>   PMyRec = ^TMyRec;
> var
>   x: PByte;
>   r: TMyRec;
> begin
>   x := @r;                // works
>   x := PMyRec(@r);  // fails
> end.


By default @ returns a *untyped* pointer that can be assigned to any
pointer type. Thus is compatible with Delphi and can be controlled with the
$TypedAddress directive (there's a slight exception to this when you assign
a pointer to a function to a function variable (etc) or pass it to such a
parameter, because the compiler has to pick the correct overload if there
are multiple functions with the same name in scope).

Regards,
Sven

>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20230809/7433a4eb/attachment.htm>


More information about the fpc-pascal mailing list