How do you pass a variable size array into a function
How do you pass a variable size array into a function
(OP)
I'm translating some legacy stuff from another language to Delphi/Pascal. It has several arrays of variable sizes. I can't figure out how the function is declared.
Codewise, I have
There are arrays of size 7 to 90. It isn't very efficient to pass in an array of size 90 every time so I'm not sure what to do. I can't use a singly linked list as the later routines access the definition by index.
Codewise, I have
CODE
type
CustomTable = record
m_kind: integer;
m_value: integer;
m_name: array[1..16] of char;
end;
{ ,-- how do I do this bit}
function DBAdd (in_defn: array [] of CustomTable,
in_numEntries: integer);
CustomTable = record
m_kind: integer;
m_value: integer;
m_name: array[1..16] of char;
end;
{ ,-- how do I do this bit}
function DBAdd (in_defn: array [] of CustomTable,
in_numEntries: integer);
RE: How do you pass a variable size array into a function
I'm waiting for the white paper entitled "Finding Employment in the Era of Occupational Irrelevancy"
RE: How do you pass a variable size array into a function
Not in old fashion Pascal.
CODE --> Object Pascal
CustomTable=record
m_kind:Integer;
m_value:Integer;
m_name:array[1..16] of Char;
end;
var
arr:array of CustomTable; { for method 1 }
tab1,tab2,tab3:CustomTable; { for method 2 }
procedure DBAdd(in_defn:array of CustomTable;in_numEntries:Integer);
var i:Integer;
begin
Writeln('I got ',Length(in_defn),' in_defn :');
for i:=0 to Length(in_defn)-1 do
Writeln(' - ',i,' : ',in_defn[i].m_value);
Writeln('And in_numEntries : ',in_numEntries);
end;
begin
{ method 1 }
SetLength(arr,3);
arr[0].m_value:=42;
arr[1].m_value:=43;
arr[2].m_value:=44;
DBAdd(arr,2010);
{ method 2 }
tab1.m_value:=52;
tab2.m_value:=53;
tab3.m_value:=54;
DBAdd([tab1,tab2,tab3],2011);
end.
Feherke.
http://free.rootshell.be/~feherke/
RE: How do you pass a variable size array into a function
feherke - thanks. I'll try that.
RE: How do you pass a variable size array into a function
I'm waiting for the white paper entitled "Finding Employment in the Era of Occupational Irrelevancy"
RE: How do you pass a variable size array into a function
With GNU Pascal you cannot declare in the main program
CODE
error: syntax error before `of'
Only the first feherke's method works (with modifications):
CODE
const
Nmax = 90;
type
CustomTable=record
m_kind:Integer;
m_value:Integer;
m_name:array[1..16] of Char;
end;
var
arr:array [1..Nmax] of CustomTable; { for method 1 }
procedure DBAdd(in_defn:array of CustomTable;in_numEntries:Integer);
var i:Integer;
begin
writeln('Lower index of in_defn = ', low(in_defn));
writeln('Upper index of in_defn = ', high(in_defn));
writeln('in_numEntries = ',in_numEntries);
for i:=low(in_defn) to in_numEntries-1 do
writeln(' - ',i,' : ',in_defn[i].m_value);
end;
begin
{ method 1 }
arr[1].m_value:=42;
arr[2].m_value:=43;
arr[3].m_value:=44;
writeln('Lower index of arr = ', low(arr));
writeln('Upper index of arr = ', high(arr));
writeln;
(* Calling the procedure *)
DBAdd(arr,3);
end.
CODE
Roman@DANKA-PC ~/pascal
$ arrays
Lower index of arr = 1
Upper index of arr = 90
Lower index of in_defn = 0
Upper index of in_defn = 89
in_numEntries = 3
- 0 : 42
- 1 : 43
- 2 : 44
RE: How do you pass a variable size array into a function
RE: How do you pass a variable size array into a function
First I pass to the same procedure an array of 3 elements and then an array of 5 elements:
CODE
type
CustomTable=record
m_kind:Integer;
m_value:Integer;
m_name:array[1..16] of Char;
end;
var
arr1:array [1..3] of CustomTable;
arr2:array [1..5] of CustomTable;
procedure DBAdd(in_defn:array of CustomTable);
var i:Integer;
begin
writeln('Lower index of in_defn = ', low(in_defn));
writeln('Upper index of in_defn = ', high(in_defn));
writeln('Number of array elements = ', high(in_defn)+1);
for i:=low(in_defn) to high(in_defn) do
writeln(' - ',i,' : ',in_defn[i].m_value);
end;
begin
(*--- 3 elements array ---*)
arr1[1].m_value:=42;
arr1[2].m_value:=43;
arr1[3].m_value:=44;
writeln('Lower index of arr = ', low(arr1));
writeln('Upper index of arr = ', high(arr1));
writeln;
DBAdd(arr1);
writeln('--------------------------------');
(*--- 5 elements array ---*)
arr2[1].m_value:=10;
arr2[2].m_value:=20;
arr2[3].m_value:=30;
arr2[4].m_value:=40;
arr2[5].m_value:=50;
writeln('Lower index of arr = ', low(arr2));
writeln('Upper index of arr = ', high(arr2));
writeln;
DBAdd(arr2);
end.
CODE
$ arrays2
Lower index of arr = 1
Upper index of arr = 3
Lower index of in_defn = 0
Upper index of in_defn = 2
Number of array elements = 3
- 0 : 42
- 1 : 43
- 2 : 44
--------------------------------
Lower index of arr = 1
Upper index of arr = 5
Lower index of in_defn = 0
Upper index of in_defn = 4
Number of array elements = 5
- 0 : 10
- 1 : 20
- 2 : 30
- 3 : 40
- 4 : 50
RE: How do you pass a variable size array into a function
RE: How do you pass a variable size array into a function
That wording may mislead beginners. FreePascal was made as compatible as possible with Delphi ( and other compilers too ), but they have nothing else common.
Feherke.
http://free.rootshell.be/~feherke/
RE: How do you pass a variable size array into a function
IMHO, FreePascal seems to be more advanced than GNU Pascal.
RE: How do you pass a variable size array into a function
Theoretically the standard should be what ISO 7185 says...
Feherke.
http://free.rootshell.be/~feherke/
RE: How do you pass a variable size array into a function
Regarding old fashion Pascal, you could use pointer :
CODE --> Pascal
CustomTable=record
m_kind:Integer;
m_value:Integer;
m_name:array[1..16] of Char;
end;
tarrayofCustomTable=array [0..0] of CustomTable;
parrayofCustomTable=^tarrayofCustomTable;
var
arr:parrayofCustomTable;
nr,i:Integer;
procedure DBAdd(in_defn:parrayofCustomTable;in_numEntries:Integer);
var i:Integer;
begin
Writeln('I got ',in_numEntries,' in_defn : ');
for i:=0 to in_numEntries-1 do
Writeln(' - ',i,' : ',in_defn^[i].m_value);
end;
begin
Randomize;
nr:=3;
if MaxAvail<SizeOf(CustomTable)*nr then begin
Writeln('Not enough memory');
Halt;
end;
GetMem(arr,SizeOf(CustomTable)*nr);
for i:=0 to nr-1 do
arr^[i].m_value:=Random(100);
DBAdd(arr,nr);
FreeMem(arr,SizeOf(CustomTable)*nr);
end.
Feherke.
http://free.rootshell.be/~feherke/
RE: How do you pass a variable size array into a function
For future reference, Delphi questions go here. forum102 While you got a number of responses here, it will benefit you to post there, as you will have more people familiar with Delphi looking in there than you will here.
The de-facto standard essentially got written by Borland through their compilers (BP/TP, earlier Delphi(<v5), and later Delphi), simply because of market acceptance and the fact that there wasn't much effort put forth from the so-called standard bearers. That's born out in that the Pascal creator even admitted Borland to have the standard.
So in most conversations, the other compilers are put up against those two. As I remember FreePascal, it is made more to Delphi standards than BP standards, though you can set it to work with either (with a few caveats, since they left 16-bitdom a long time ago).
Truthfully, if anyone uses a semi-modern compiler, the question of "Delphi or Pascal" is moot. But there are enough people using different/older stuff for whatever reason (through VMs or boot disks or the like because of equipment, old software, etc) that it is still a worthy question to ask.
I'm waiting for the white paper entitled "Finding Employment in the Era of Occupational Irrelevancy"
RE: How do you pass a variable size array into a function
http://www.tek-tips.com/threadminder.cfm?pid=102
I'm waiting for the white paper entitled "Finding Employment in the Era of Occupational Irrelevancy"
RE: How do you pass a variable size array into a function
RE: How do you pass a variable size array into a function
RE: How do you pass a variable size array into a function