INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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);
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.

RE: How do you pass a variable size array into a function

So is it Delphi or Pascal?  That matters for the answer to your question, as well as what the proper forum is for your question.

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

Hi

Not in old fashion Pascal.

CODE --> Object Pascal

  type
    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.
Tested with FreePascal and Kylix.

Feherke.
http://free.rootshell.be/~feherke/

RE: How do you pass a variable size array into a function

(OP)
Glen9999 - I've just been told by management that it has to be in Pascal.  Since I had Delphi installed on my machine and it has a Pascal-ish compiler, I decided to use it.  Then I got stuck.

feherke - thanks.  I'll try that.

RE: How do you pass a variable size array into a function

xwb: True Pascal is much different than Delphi.  Case in point is what Feherke posted.  It's a great Delphi answer (FreePascal and Kylix are both Delphi variants), but fails in true Pascal (e.g. Borland/Turbo Pascal and equivalents).  Hence, it's necessary to know which is the case.

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

I tried feherke's example with the GNU Pascal compiler which is probably more conservative than FreePascal.
With GNU Pascal you cannot declare in the main program

CODE

arr:array of CustomTable;   { for method 1 }
because you get the error:
error: syntax error before `of'

Only the first feherke's method works (with modifications):

CODE

program arrays;
  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

$ gpc arrays.pas -o arrays

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

(OP)
Thanks mikrom.  The only problem I'm trying to avoid having to create a whole load of arrays of the same size.

RE: How do you pass a variable size array into a function

Quote (xwb):


I'm trying to avoid having to create a whole load of arrays of the same size
You don't need to use arrays of the same size. You can pass to the procedure arrays of several sizes which fits your needs - see this:
First I pass to the same procedure an array of 3 elements and then an array of 5 elements:  

CODE

program arrays2;
  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.
Output:

CODE

$ gpc arrays2.pas -o arrays2

$ 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

(OP)
Thanks for the tips - it is Delphi.  The boss doesn't know the difference between Delphi and Pascal.  

RE: How do you pass a variable size array into a function

Hi

Quote (Glenn9999):

FreePascal and Kylix are both Delphi variants
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

I thought that FreePascal is closer to Turbo-,Borland-Pascal and Delphi and GNU Pascal is closer to the "Standard Pascal". But I'm not sure if there is an standard for Pascal smile

IMHO, FreePascal seems to be more advanced than GNU Pascal.
 

RE: How do you pass a variable size array into a function

Hi

Quote (mikrom):

But I'm not sure if there is an standard for Pascal
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

Hi

Regarding old fashion Pascal, you could use pointer :

CODE --> Pascal

  type
    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.
Tested with Turbo Pascal.

Feherke.
http://free.rootshell.be/~feherke/

RE: How do you pass a variable size array into a function

Quote (xwb):


Thanks for the tips - it is Delphi.  The boss doesn't know the difference between Delphi and Pascal.  

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.

Quote (mikrom):


I thought that FreePascal is closer to Turbo-,Borland-Pascal and Delphi and GNU Pascal is closer to the "Standard Pascal". But I'm not sure if there is an standard for Pascal

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

I see now the TGML forum tag reference didn't get parsed.  So here's the regular URL for the Delphi forum here.

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

For what it's worth, in old-fashioned turbo-pascal I used to define a template array of enormous length as a 'type', and then pass a pointer to the genuine array, and type-cast it using my template array-type. Dreadful, BAD style, I know, but I've always had a soft-spot for leaving all my pointers untyped and type-casting, and in my defence, no one ever taught me proper style.
 

RE: How do you pass a variable size array into a function

(OP)

Quote (lonehill):


 I used to define a template array of enormous length as a 'type', and then pass a pointer to the genuine array, and type-cast it using my template array-type
That is what I used to do in the late 70s when Pascal was just a popular teaching language.  Just remember that I had no end of problems with parameter passing because of the strong typing.

RE: How do you pass a variable size array into a function

breathes sigh of relief that it's not just me who indulges/indulged in this sort of practice, blushes, and admits to rather liking the old days of weak typing...

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close