Forum > General
multi-dimensional open array parameter?
photor:
--- Quote from: User137 on May 31, 2014, 01:42:42 pm ---Was there something wrong with taazz code? I can give another example
--- Code: ---type
TArr = array of array of integer;
...
procedure Test(arr: TArr);
begin
arr[high(arr), high(arr[0])]:=1;
end;
procedure TForm1.FormCreate(Sender: TObject);
var arr: TArr;
begin
setlength(arr, 10, 20); // 10x20 matrix
arr[9, 19]:=0;
Test(arr);
caption:=inttostr(arr[9, 19]); // Sets caption 1
end;
--- End code ---
In nowhere did i tell Test() function that the matrix size was 10x20, but it could still modify the last value.
--- End quote ---
I mean using a CONSTANT matrix, say
--- Code: ---ar2:array[0..1,0..1] of Integer=((2,-1),(-3,2));
--- End code ---
as the input of a function.
photor:
--- Quote from: Leledumbo on May 31, 2014, 01:43:28 pm ---
--- Quote from: photor on May 31, 2014, 12:17:50 pm ---Thanks for all the warm replies. But it is a pity that free pascal doesn't support multi-dimensional open array parameters. To me, extending the function of one-dimensional open array parameters to the multi-dimensional case is not a big challenge to the free pascal developers.
--- End quote ---
If you think so, then implement. Welcome to open source world.
--- End quote ---
Free pascal is too huge a project for me. I completely don't know where in the source code I can edit.
Leledumbo:
--- Quote from: photor on May 31, 2014, 02:41:54 pm ---
--- Quote from: Leledumbo on May 31, 2014, 01:43:28 pm ---
--- Quote from: photor on May 31, 2014, 12:17:50 pm ---Thanks for all the warm replies. But it is a pity that free pascal doesn't support multi-dimensional open array parameters. To me, extending the function of one-dimensional open array parameters to the multi-dimensional case is not a big challenge to the free pascal developers.
--- End quote ---
If you think so, then implement. Welcome to open source world.
--- End quote ---
Free pascal is too huge a project for me. I completely don't know where in the source code I can edit.
--- End quote ---
Then saying it's not a big challenge is not appropriate. There are implementation details that prevent this from being implemented. Note that open array accepts ANY kind of array, static or dynamic, even partial. So how would you pass a multidimensional static and dynamic array and treat them being equal? Both have different internal representation. A static array, no matter how many dimensions, is still implemented as a flat memory data. Dynamic array, OTOH, when used with >1 dimension is an array of pointers (even as a single dimension, it's internally a pointer). Therefore, it can't be treated as flat memory data because each row is NOT adjacent. It still works for single dimension because the treatment could be equal as flat memory data.
howardpc:
--- Quote from: photor on May 31, 2014, 02:38:49 pm ---I mean using a CONSTANT matrix, say
--- Code: ---ar2:array[0..1,0..1] of Integer=((2,-1),(-3,2));
--- End code ---
as the input of a function.
--- End quote ---
This is possible in Pascal using a user-defined type, as Juha pointed out. Such as this:
--- Code: ---program ConstantMatrix;
type
T2DArray = array[0..1,0..1] of integer;
const
Array2D: T2DArray = ((2,-1),(-3,2));
procedure SqrArray(var anArray: T2DArray);
var
i, j: integer;
begin
for i:=0 to 1 do
for j:=0 to 1 do
anArray[i,j]:=sqr(anArray[i,j]);
end;
procedure DisplayArray(anArray: T2DArray; const aTitle:string);
var
i, j: integer;
begin
WriteLn(aTitle);
for i:=0 to 1 do begin
for j:=0 to 1 do
Write(anArray[i][j]:3);
WriteLn;
end;
end;
var
ar2:T2DArray;
begin
ar2:=Array2D;
DisplayArray(ar2,'Before:');
SqrArray(ar2);
DisplayArray(ar2,'After SqrArray():');
ReadLn;
end.
--- End code ---
mse:
If solely the first dimension is variable it is possible:
--- Code: ---type
arty = array[0..1] of integer;
procedure test(p: array of arty);
begin
end;
const
ar2: array[0..1] of arty = ((2,-1),(-3,2));
procedure test2();
begin
test(ar2);
end;
--- End code ---
Or use an open type parameter:
--- Code: ---const
ar1: array[0..1,0..1] of integer = ((2,-1),(-3,2));
procedure test3(const ar; const high1: integer; const high2: integer);
begin
end;
procedure test4();
begin
test3(ar1,high(ar1),high(ar1[0]));
end;
--- End code ---
Navigation
[0] Message Index
[#] Next page
[*] Previous page