comp.lang.ada
 help / color / mirror / Atom feed
From: tmoran@acm.org
Subject: Re: Arrays & pointers question
Date: Tue, 15 Nov 2005 13:53:56 -0600
Date: 2005-11-15T13:53:56-06:00	[thread overview]
Message-ID: <ze-dnWZAyLBJoefeRVn-hQ@comcast.com> (raw)
In-Reply-To: 1132082934.714260.258260@g43g2000cwa.googlegroups.com

>   type MYTYPE is array (1 .. 10) of INTEGER;
>   X1: MYTYPE;
>   X2: MYTYPE;
>   X3: MYTYPE;
>
>  I have a SORT procedure that takes 2 parameters: the first is the
> number of entries,
> and the 2nd is the address of the array.
>
> What's the correct syntax for the procedure declaration, the procedure
> call, and the references to the array elements inside the procedure?

    Arrays in Ada are not pointers or addresses but rather objects, so
you need to pass the object in, sort it, and pass it out (don't worry,
the compiler is smart enough to generate code that passes references even
though logically it's copying the array).

    Also, arrays carry their own upper and lower boundaries, so you
don't need to pass a count.

So declare your procedure this way:
  procedure Sort(This_Array : in out MyType) is
  begin
    for i in This_Array'range loop
      for j in This_Array'first .. i-1 loop
        if This_Array(i) < This_Array(j) then ....
  ...
and call it like this:
  Sort(X1);

Note that in this case you've declared any array of type MyType to have
exactly 10 elements, so the compiler doesn't even need to pass the count,
and it can generate code knowing that This_Array'range is 1 ..  10 and
This_Array'first is 1.

If you want to sort different length arrays, try

    type MYTYPE is array (Integer range <>) of INTEGER;
    X1: MYTYPE(1 .. 10);
    X2: MYTYPE(1 .. 20000);

The declaration and call of Sort are unchanged.  Since the parameter to
Sort has bounds specified at run time, you also have the option of
sorting just a slice of a MYTYPE array, eg the first ten elements of X2:
   Sort(X2(1 .. 10));
or the last ten elements:
   Sort(X2(19991 .. 20000));



  reply	other threads:[~2005-11-15 19:53 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-15 19:28 Arrays & pointers question csaagpDIESPAMDIE
2005-11-15 19:53 ` tmoran [this message]
2005-11-15 20:31   ` csaagpDIESPAMDIE
2005-11-15 21:03     ` Marc A. Criley
2005-11-16 13:18       ` csaagpDIESPAMDIE
2005-11-16 13:37         ` Marc A. Criley
2005-11-16 18:37           ` csaagpDIESPAMDIE
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox