[fpc-pascal] Are record fields zeroed on declaration of a record?

Michael Van Canneyt michael at freepascal.org
Fri Oct 9 09:29:36 CEST 2020



On Fri, 9 Oct 2020, Bo Berglund via fpc-pascal wrote:

> On Thu, 8 Oct 2020 23:00:32 +0200 (CEST), Michael Van Canneyt via
> fpc-pascal
> <fpc-pascal at lists.freepascal.org> wrote:
>
>>
>>
>> On Thu, 8 Oct 2020, Ryan Joseph via fpc-pascal wrote:
>>
>>>
>>>
>>>> On Oct 8, 2020, at 5:14 AM, gabor via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:
>>>> 
>>>> You can define typed constant and assign it to variable.
>>>> 
>>>> type
>>>>  TMyRecord = record
>>>>    X, Y: Integer;
>>>>    S: String;
>>>>  end;
>>>> 
>>>> const
>>>>  MYDEFREC: TMyRecord = (X: 1; Y: 2; S: 'ABC');
>>>
>>> I never understood why we can't initialize fields at init time for
>>> records.  Pascal seems to be the only language that doesn't support this
>>> in 2020.  Thinking of C++, C#, PHP, Swift and many more all let you do
>>> this.  Not even Delphi supports this AKAIK.
>>
>> Of course you can:
>>
>> var
>>   x : record a,b : integer; end = (a:1;b:3);
>>
>>
>> begin
>> end.
>>
>> This is identical to how C++ does it. From cppreference.com:
>>
>> typedef struct { int k; int l; int a[2]; } T;
>> T x = {.l = 43, .k = 42, .a[1] = 19, .a[0] = 18 };
>>
>
> If you do that can you be selective and initialize only some fields or
> must you list all or none?

You can do selected fields.
Fields you didn't specify are undefined.

But the code you posted is wrong.

Michael.


More information about the fpc-pascal mailing list