Paer Toernell
asked on
Passing a dynamic array in a parameter
This is what i have been trying:
but i get the error E2010 Incompatible types: 'MyArray' and 'dynamic array'
What i want to do is pass the dynamic array as a pointer - its huge so i don't want a copy of it.
type
MyArray = array of integer;
function Test ( a : MyArray):boolean;
begin
end;
procedure DoTheTest;
var
b : array of integer;
begin
Test(b);
end;
but i get the error E2010 Incompatible types: 'MyArray' and 'dynamic array'
What i want to do is pass the dynamic array as a pointer - its huge so i don't want a copy of it.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Also you can cast it:
type
MyArray = array of integer;
function Test ( a : MyArray):boolean;
begin
end;
procedure DoTheTest;
var
b : array of integer;
begin
if Test(MyArray(b)) then
//ShowMessage('Works');
end;
I don't know how will you use the functions, so I just add some important lines before declaring the array.
function Test ( a : MyArray):boolean;
begin
SetLength(a, length_of_your_dynamic_arr ay); //first line <must>
...
end;
function Test ( a : MyArray):boolean;
begin
SetLength(a, length_of_your_dynamic_arr
...
end;
You can use a pointer or a var parameter, either of these will send the address of the array
type
PMyArray = ^TMyArray;
TMyArray = array of Integer;
//Method 1
function Test(A: PMyArray):boolean;
begin
end;
procedure DoTheTest;
var
b : TMyArray;
begin
Test(@b);
end;
//Method 2
function Test(var A: TMyArray):boolean;
begin
end;
procedure DoTheTest;
var
b : TMyArray;
begin
Test(b);
end;
Open in new window