Is a dynamic array of Char allowed when the parameter type is open array of Char?

后端 未结 3 2039
时光说笑
时光说笑 2020-12-31 10:57

I was looking at Delphi: array of Char and TCharArray "Incompatible Types" and started experimenting. What I discovered is rather interesting.

proc         


        
3条回答
  •  半阙折子戏
    2020-12-31 11:45

    I think the reason is that array of Char is compatible with PChar, as this code does compile:

    procedure Clear(AArray: array of Char);
    var
      I: Integer;
    begin
      for I := Low(AArray) to High(AArray) do
        AArray[I] := #0;
    end;
    
    var
      MyArray: array of Char;
      P: PChar;
    begin
      Clear(P^);
    end.
    

    That is probably for historic reasons.
    Hopefully Barry Kelly or Danny Thorpe will kick in and provide some more feedback on this.

    --jeroen

提交回复
热议问题