Board index » delphi » Passing arrays to procedures

Passing arrays to procedures

On Tue, 25 Mar 1997 16:58:14 +0100, ReBeL <re...@chim03.chin.unipd.it>
wrote:

Quote
>type
>   sm = array [0..30000] of integer;

> var
>   Data : ^sm;

> procedure Calc (var x:array of ^Integer); <--- Work ?????
> blah end;

No work, actually...this is defined as an array of, which is a no-no
in parameter passing....gonna have to define that part as a type as
well...

Glenn Grotzinger
Web Page: http://www.geocities.com/Paris/3537
Writer of the Excellent Training Manual known as the TP Tutorial.
To email, if you hit the reply button, delete the {remove_this}
out of the replied message.  Just some protection from SPAM. :(

 

Re:Passing arrays to procedures


How do I pass an array to a procedure in a single file program.
eg:
type
  sm = array [0..30000] of integer;

var
  Data : ^sm;

procedure Calc (var x: ^sm);     <---- error with type???
blah end;

Help me or be damned for all eternity by Falgerfes the god of pascal.
--
______________________________
Adam Anderson. Comms Tech.
Email must contain Adam97 in
Subject or it will be deleted.

Re:Passing arrays to procedures


Quote
On Tue, 25 Mar 1997, Adam Anderson wrote:
> How do I pass an array to a procedure in a single file program.
> eg:
> type
>   sm = array [0..30000] of integer;

> var
>   Data : ^sm;

> procedure Calc (var x: ^sm);     <---- error with type???
> blah end;

> Help me or be damned for all eternity by Falgerfes the god of pascal.
> --
> ______________________________
> Adam Anderson. Comms Tech.
> Email must contain Adam97 in
> Subject or it will be deleted.

Ex:
var
type
   sm = array [0..30000] of integer;

 var
   Data : ^sm;

 procedure Calc (var x:array of ^Integer); <--- Work ?????
 blah end;

begin
 Calc (Data)
end
***************************************************************************
*               -* ReBeL *- -* ReBeL *- -* ReBeL *-                       *
*                                                                         *
*             System Administrator @ chim03.chin.unipd.it                 *
*________________________________________________________________________ *
*                                                |                        *
*                           e-mail:              |                        *
*       ITALY  :    re...@chim03.chin.unipd.it   |                        *
*       FINLAND:    i...@freenet.hut.fi          |                        *
*       USA    :    ro...@deathsdoor.com         | Waiting For The Sun    *
*                   i...@bbs.gaianet.net          |           (Doors)      *
*------------------------------------------------]                        *
*                                                |                        *
*                  HomePage:                     |                        *
*     http://chim03.chin.unipd.it/~rebel         |                        *
*  http://www.netforward.com/deathsdoor/?rosso   |                        *
***************************************************************************

Re:Passing arrays to procedures


Try the following:

type
  sm = array [0..30000] of integer;
  psm = ^sm;
   { types used for procedure declarations must be defined beforehand }

var
  Data: psm

procedure Calc (var x: psm);    
Begin
  blah
end;

In article <3338D09F....@usa.net>, not...@usa.net says...

Quote

>How do I pass an array to a procedure in a single file program.
>eg:
>type
>  sm = array [0..30000] of integer;

>var
>  Data : ^sm;

>procedure Calc (var x: ^sm);     <---- error with type???
>blah end;

>Help me or be damned for all eternity by Falgerfes the god of pascal.
>--
>______________________________
>Adam Anderson. Comms Tech.
>Email must contain Adam97 in
>Subject or it will be deleted.

--
Views expressed herein are not necessarily the views
of ONTRACK Data International, Inc.
*******************************************************************
* Ken Stieers                |  Minneapolis, Los Angeles          *
* AV Research/Apps. Eng.     |  Washington, DC  - 1.800.872.2599  *
* ONTRACK Data International |  London - 0800.24.39.96            *
* Ontrack Data Recovery      |  Japan - 81.429.32.6365            *
* Eden Prairie, MN           |  Stuttgart - 0130.815.198          *
*******************************************************************

Re:Passing arrays to procedures


In article <333580a7.290...@news.internetland.net>,

Quote
Programmer Dude <ggrotz{remove_th...@internetland.net> wrote:
>On Tue, 25 Mar 1997 16:58:14 +0100, ReBeL <re...@chim03.chin.unipd.it>
>wrote:

>>type
>>   sm = array [0..30000] of integer;

>> var
>>   Data : ^sm;

>> procedure Calc (var x:array of ^Integer); <--- Work ?????
>> blah end;

>No work, actually...this is defined as an array of, which is a no-no
>in parameter passing....gonna have to define that part as a type as
>well...

No... you can declare an array of something in the paramater list of
a procedure.  ie, procedure foo(var x:array of integer); works fine.
However, the above is incorrect in that you wouldn't do array of ^integer.
If the array you are passing in is an array of pointers to integers, you
should just declare it as: procedure Calc(var x:array of pointer); and
then typecast them to integers.  So, you could do:

procedure Calc(var x:array of pointer);
begin
 integer(x[10]^):=42;
end;

However, for the above, you would indeed need to make a separate type
so that you don't get a type mismatch.

--Mark Iuzzolino
one of the monst...@monstersoft.com  |  "I'd love to go out with you,
http://www.monstersoft.com           |   but I have to floss my cat."

Re:Passing arrays to procedures


Quote
Ken Stieers wrote:
> Try the following:

> type
>   sm = array [0..30000] of integer;
>   psm = ^sm;
>    { types used for procedure declarations must be defined beforehand }

> var
>   Data: psm

> procedure Calc (var x: psm);
> Begin
>   blah
> end;

What you get here if you do that is a double indirection of pointers (a
var parameter is passed as an address, so "var x : psm" would be a
pointer to a pointer type).  You can use type psm to declare dynamic
structures on the heap, but when you create the calc() procedure, this
would be a bit more efficient:

procedure calc (var x : sm);

begin
  {...}
end;

Within the procedure you work with x; calling from outside, you'd pass
Data^.

Quote
> * Ken Stieers                |  Minneapolis, Los Angeles          *

--
Scott Earnest        | We now return you to our regularly |
set...@ix.netcom.com | scheduled chaos and mayhem. . . .  |

Re:Passing arrays to procedures


Adam Anderson <not...@usa.net> wrote in article <3338D09F....@usa.net>...

Quote
> How do I pass an array to a procedure in a single file program.
> eg:
> type
>   sm = array [0..30000] of integer;

> var
>   Data : ^sm;

> procedure Calc (var x: ^sm);     <---- error with type???
> blah end;

> Help me or be damned for all eternity by Falgerfes the god of pascal.
> --

Create a data type that is a pointer to the array.

--
----------------------------------------------------------------------------
----
?                    mail to : mre...@ici.net                      ?
?  http://www.ici.net/cust_pages/mrealm/BANDP.HTM ?
----------------------------------------------------------------------------
-----
' It is better to die on your feet, than to live on your knees! '

Quote

Re:Passing arrays to procedures


Quote
ReBeL <re...@chim03.chin.unipd.it> wrote:

Yo, ReBel!  Do you realize that with wordwrap your sig comes to 33
lines!  I realize my screen wrapping at 60 characters is a tad
short, but even w/o wordwrap its still 17 lines of 75 characters
each!  Our UK friends are going to love paying for the extra 1300
characters just to hear from you. <NOT!>

BTW, if you had fed your example to the compiler you would have seen
that it wouldn't work. <g>

Now, on to helping Adam.

Quote
>On Tue, 25 Mar 1997, Adam Anderson wrote:

>> How do I pass an array to a procedure in a single file program.
>> eg:
>> type
>>   sm = array [0..30000] of integer;

>> var
>>   Data : ^sm;

>> procedure Calc (var x: ^sm);     <---- error with type???

Simply use (VAR x: sm) to pass the array by reference.  
You can then pass "data" using Calc(Data^).
{ Calc will use x[subscript] to access the array }

The other choice is to define a pointer type to the array.
Ex.  TYPE tpSM = ^SM;

Then use this type whenever you need to specify a pointer to the
array.

Ex. VAR Data: tpSM;

Procedure Calc(x: tpSM);
{ Calc will have to use x^[subscript] to access the array }

Call using  Calc(Data);

If you are certain that you will never try to pass a nil pointer,
then the first method using a VAR parameter is okay.  However, if
Calc needs to test for a nil array, the second method using a
pointer (tpSM) is the only way to go.  Of course you can always use
the second method.

Quote
>> Help me or be damned for all eternity by Falgerfes the god of pascal.

OH NO!  Too many people being damned here!  Maybe I should withdraw
my help. Or perhaps rewrite it in a more technical manner so it will
be harder to understand?  Then again I could just leave you with
ReBel's incorrect answer.  :->

    ...red

Re:Passing arrays to procedures


In article <3338D09F....@usa.net>, not...@usa.net says...

Quote

>How do I pass an array to a procedure in a single file program.
>eg:
>type
>  sm = array [0..30000] of integer;

>var
>  Data : ^sm;

>procedure Calc (var x: ^sm);     <---- error with type???
>blah end;

This is confusing.  Do you want the procedure to have a pointer to
the array or a pointer to the pointer to the array?  If you want
it to have a pointer to the array:

procedure Calc(var x: sm);
begin
   blah;
end;

I presume you have a New(Data) to create the actual array.  To
pass it to the procedure:

   Calc(sm^);

Notice where the ^ is.  

--
R. N. (dick) Wisan  - Email: wis...@norwich.net
                    - Snail: 37 Clinton Street, Oneonta NY 13820, U.S.A.
                    - Just your opinion, please, ma'am: No fax.

Other Threads