How to initialize a static array?

后端 未结 3 1872
鱼传尺愫
鱼传尺愫 2021-01-06 08:50

Let\'s say I have this declaration:

TYPE
 RDisk= packed record
   R2: array[1..1024] of Byte;
   etc
   etc
  end;

How do I initialize R2 t

相关标签:
3条回答
  • 2021-01-06 09:03

    You can declare a constant of type R2 and initialize it to all zeros like this:

    const
      zeros: R2 = (0, 0, 0, ...);
    

    The array length is 1024, so you must specify all 1024 comma-separated values in that list.

    0 讨论(0)
  • 2021-01-06 09:08

    You can initialize an array with zero using this

    ZeroMemory(@R2[0],length(R2));
    

    or this

    FillChar(R2[0],length(R2),0); 
    
    0 讨论(0)
  • 2021-01-06 09:14

    Omit the fields you want to zero:

    type
     RDisk= packed record
       R2: array[1..512] of Byte;
       I: Integer;
       D: Double;
       R3: array[1..512] of Byte;
      end;
    
    const
     Disk: RDisk=
       (
        I: 3;
        D: 2.5;
       );
    

    or,

    const
     Disk: RDisk=
       (
       );
    


    I don't know why it works, it doesn't quite fit in Record Constants' documentation.

    0 讨论(0)
提交回复
热议问题