views:

123

answers:

1

I wrote a function that gives me the length of a dynamic array by converting it to string and asking length(trim(string));

function arraylength(a: array of char): integer;
var i: integer;
s: string;
begin
    for i:=0 to high(a) do
  begin
  s[i] := a[i-1];
    Result := length(trim(s));
  end;
end;

In my main program i read text into a string, convert it to array

procedure TForm1.Button2Click(Sender: TObject);
var i: integer;
begin
  for i:=0 to length(sString) do
  begin
    cChar[i] := sString[i];
  end;
end;

and do:

ShowMessage(IntToStr(arraylength(cChar)));

I get the error as stated in the title.

+4  A: 

When passing arrays to procedures and functions in delphi you should declare them as a separate type. Thus:

type
  MyArray =  array of char;

and then

function arraylength(a: MyArray ): integer;

BTW: why aren't you using built-in functions like Length() ? In Delphi2009 type string is unicode string, so Length returns Length in characters, not in bytes.

smok1
Im not using it because in that case I have a dynamic array and length(cChar) returns something bigger than the actual count of characters in the array due to the dynamic allocation of space. A function that gives me the ACTUAL count of characters would be highly appreciated.
Acron
You mean you have array of say 20 elements, buy you only use 15 at the moment?
smok1
That is absolutely true.
Acron