Board index » delphi » Passing Arrays to Procedures and Functions

Passing Arrays to Procedures and Functions

Hello,

Is there away in Turbo Pascal 5.5 to pass an array to a procedure or
function?
And can you shrink or grow an array?  Say I want to load a file into an
array but I never know how large the file will be, without having to set the
array to the maximum size, how can this be accomplished?

Thanks in advance
EMail: mailto:mjgr...@netcom.com
Homepage: http://www.geocities.com/SiliconValley/Heights/3895/

 

Re:Passing Arrays to Procedures and Functions


Quote
Michael Gregg wrote:
> Hello,

> Is there away in Turbo Pascal 5.5 to pass an array to a procedure or
> function?
> And can you shrink or grow an array?  Say I want to load a file into an
> array but I never know how large the file will be, without having to set the
> array to the maximum size, how can this be accomplished?

The simple answer is using pointer.
After reading the file, the procedure may return the pointer to the start of the
buffer and its size, so the caller knows what is returning from the procedure.

Re:Passing Arrays to Procedures and Functions


Quote
> Hello,

> Is there away in Turbo Pascal 5.5 to pass an array to a procedure or
> function?
> And can you shrink or grow an array?  Say I want to load a file into an
> array but I never know how large the file will be, without having to set the
> array to the maximum size, how can this be accomplished?

> Thanks in advance

The most simple and straightforward way is to use an array of pointers.
The same is done in the Turbo-Vision Collection object and in Windows.
You can allocate an array which is surely big enough to hold the number
of pointers, up to 16000 pointers can be in one array.

Type PMyRecTyp = ^TMyRecTyp;
         TMyRecTyp = Record
                                     Name : String;
                                     Adress : String;
                                      etc....
                                 End;
        TMyRecArr = Array[0..3999] of PMyRecTyp;

Var MyRecords : TMyRecArr; {shall be fillchared at start to 0}
       CountPers  : Integer;    {must be init'ed at start to 0}

Procedure AddRecord(Var PersonRec:TMyRecTyp);
begin
   New(MyRecords[CountPers];  {obtain memory from the heap}
   Inc(CountPers);
   MyRecord[CountPers]^ := PersonRec;
end;

Another way is using a chain, where in the record is a pointer to the
next person. This chain can have two pointers too: next and previous.
But this is not a modern method.

Franz Glaser http://members.eunet.at/meg-glaser

Other Threads