Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Array type required? But you have it!

682 views
Skip to first unread message

Ian Boyd

unread,
Nov 25, 2002, 10:00:41 PM11/25/02
to
Consider the following:

type
TByteDynArray = array of Byte; // <--This is in Delphi 7
types.pas
PByteDynArray = ^TByteDynArray;

function DoStuff(I: PByteDynArray): Byte;
begin
Result := I[0];
end;

Delphi 7 gives me the error: Array type required

Now why can't Delphi figure out the array type?


John Herbster \(TeamB\)

unread,
Nov 25, 2002, 10:23:37 PM11/25/02
to
"Ian Boyd" <ian.ms...@avatopia.com> wrote

If you haven't figured it out already:
Maybe because I is a pointer type?
Why don't you use I^[0] instead of I[0]?
Regards, JohnH


Rudy Velthuis (TeamB)

unread,
Nov 26, 2002, 8:13:15 AM11/26/02
to
In article <3de2...@newsgroups.borland.com>, Ian Boyd says...

> Consider the following:
>
> type
> TByteDynArray = array of Byte; // <--This is in Delphi 7
> types.pas
> PByteDynArray = ^TByteDynArray;
>
> function DoStuff(I: PByteDynArray): Byte;
> begin
> Result := I[0];
> end;
>
> Delphi 7 gives me the error: Array type required

You give it a pointer to an array type, not an array type. Compilers
only do what you tell them, not what you want to tell them. So you must
pass @MyArray, and do:

So do:

if (I <> nil) and (Length(I^) > 0) then
Result := I^[0];

But why don't you simply pass a TByteDynArray instead?

function DoStuff(I: TByteDynArray): Byte;
begin
if Length(I) > 0 then


Result := I[0];
end;

Now you can simply pass the array.
--
Rudy Velthuis (TeamB)

"We should leave our minds open, but not so open that our brains
fall out." - Alan Ross Anderson

Ian Boyd

unread,
Nov 28, 2002, 9:43:16 PM11/28/02
to
> Result := I^[0];
i had long since ignored the need in Delphi to dereference pointers, cause a
pointer to a structure didn't require it (oops).

> But why don't you simply pass a TByteDynArray instead?

Yeah, i figured this out later. A lot of careful reading about untyped
constant parameters, open array parameters, dynamic array parameters,
reference counting, etc.

Thanks for the reply.


"Rudy Velthuis (TeamB)" <rvel...@gmx.de> wrote in message
news:3de37358$1...@newsgroups.borland.com...

0 new messages