[fpc-pascal] Initialization of constant record member of pointer type
Sven Barth
pascaldragon at googlemail.com
Tue Dec 1 11:05:45 CET 2020
LacaK via fpc-pascal <fpc-pascal at lists.freepascal.org> schrieb am Di., 1.
Dez. 2020, 11:00:
>
> Dňa 30.11.2020 o 23:26 Sven Barth via fpc-pascal napísal(a):
> > Am 30.11.2020 um 13:20 schrieb LacaK via fpc-pascal:
> >> Hi,
> >>
> >> is there a way how to initialize record member of pointer type (other
> >> than PChar) in the following example:
> >>
> >> type
> >> TMyRec=record
> >> a: PByte; // if I change PByte to PAnsiChar then it works
> >> end;
> >>
> >> const
> >> MyConst: TMyRec = (a: 'abcd'); // I want to a points to static
> >> memory where 'abcd' is stored
> >>
> >> I would like to initialize a to be pointer to another known constant
> >> or directly to supplied string constant.
> >>
> >> For example something like:
> >>
> >> const
> >> MyConst1='abcd';
> >> MyConst2=TMyRec = (a: @MyConst1);
> >
> > Untyped constants don't have an address you can take. You need to use
> > a typed constant:
> >
> > === code begin ===
> >
> > const
> > MyConst1: Byte = 123;
> > MyConst2: TMyRec = (a: @MyConst1);
> >
> > === code end ===
> >
> Thank you, yes it works. I have used:
>
> const
> MyConst1: AnsiString = 'abc'
> MyConst2: TMyRec = (a: @MyConst1[1]);
>
> unfortunately it seems, that I can not use Length(MyConst1) in:
>
> type
> TMyRec=record
> l: integer;
> a: PByte;
> end;
>
> const
> MyConst1: AnsiString = 'abc';
> MyConst2: TMyRec = (l: Length(MyConst1); a: @MyConst1[1]);
>
> Why is it prohibited?
>
Because MyConst1 is not an *untyped* constant. Only untyped constants can
be used in constant expressions (a pointer to something can be considered
an untyped constant).
The following might work though I did not test it:
=== code begin ===
const
MyStr = 'abc'
MyConst1: AnsiString = MyStr;
MyConst2: TMyRec = (l: Length(MyStr); a: @MyConst1[1]);
=== code end ===
Regards,
Sven
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20201201/bd7efab7/attachment.htm>
More information about the fpc-pascal
mailing list