comp.lang.ada
 help / color / mirror / Atom feed
* Arrays & pointers question
@ 2005-11-15 19:28 csaagpDIESPAMDIE
  2005-11-15 19:53 ` tmoran
  0 siblings, 1 reply; 7+ messages in thread
From: csaagpDIESPAMDIE @ 2005-11-15 19:28 UTC (permalink / raw)


Not used to pointers & Ada so I'd appreciate some help.

I have a simple sort routine that sorts an array of integers.
I have several variables of the same array type, so I'd like to pass in
the address of each array to the sort routine.

if my type/variable declarations are as such:

  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?

thanks.




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Arrays & pointers question
  2005-11-15 19:28 Arrays & pointers question csaagpDIESPAMDIE
@ 2005-11-15 19:53 ` tmoran
  2005-11-15 20:31   ` csaagpDIESPAMDIE
  0 siblings, 1 reply; 7+ messages in thread
From: tmoran @ 2005-11-15 19:53 UTC (permalink / raw)


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



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Arrays & pointers question
  2005-11-15 19:53 ` tmoran
@ 2005-11-15 20:31   ` csaagpDIESPAMDIE
  2005-11-15 21:03     ` Marc A. Criley
  0 siblings, 1 reply; 7+ messages in thread
From: csaagpDIESPAMDIE @ 2005-11-15 20:31 UTC (permalink / raw)


Thanks, that worked great.

After several tries I was able to cobble together a version that used
access type & 'aliased' etc but your solution reads much cleaner.  Plus
I didn't know the compiler handled the generated code efficiently.

My 1..10 example was just for simplicity - I have different # entries
in each array so I need a count.




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Arrays & pointers question
  2005-11-15 20:31   ` csaagpDIESPAMDIE
@ 2005-11-15 21:03     ` Marc A. Criley
  2005-11-16 13:18       ` csaagpDIESPAMDIE
  0 siblings, 1 reply; 7+ messages in thread
From: Marc A. Criley @ 2005-11-15 21:03 UTC (permalink / raw)


csaagpDIESPAMDIE@yahoo.com wrote:

> My 1..10 example was just for simplicity - I have different # entries
> in each array so I need a count.

But as you saw in Tom's example, you don't.  I hope you understood how 
'Range works as he illustrated.  It's a very powerful thing to know.

-- Marc A. Criley
-- McKae Technologies
-- www.mckae.com
-- DTraq - XPath In Ada - XML EZ Out



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Arrays & pointers question
  2005-11-15 21:03     ` Marc A. Criley
@ 2005-11-16 13:18       ` csaagpDIESPAMDIE
  2005-11-16 13:37         ` Marc A. Criley
  0 siblings, 1 reply; 7+ messages in thread
From: csaagpDIESPAMDIE @ 2005-11-16 13:18 UTC (permalink / raw)


All objects of my array type have the same dimensions; not all objects
fill out the same items.
All of my array objects have the same 'first value, the same 'last
value and the same 'range,
but they all do not fill out their arrays to the 'last value.




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Arrays & pointers question
  2005-11-16 13:18       ` csaagpDIESPAMDIE
@ 2005-11-16 13:37         ` Marc A. Criley
  2005-11-16 18:37           ` csaagpDIESPAMDIE
  0 siblings, 1 reply; 7+ messages in thread
From: Marc A. Criley @ 2005-11-16 13:37 UTC (permalink / raw)


csaagpDIESPAMDIE@yahoo.com wrote:
> All objects of my array type have the same dimensions; not all objects
> fill out the same items.
> All of my array objects have the same 'first value, the same 'last
> value and the same 'range,
> but they all do not fill out their arrays to the 'last value.
> 

That's why Tom also mentioned slicing the array that you pass to your 
function:

Given:
    X2: MYTYPE(1 .. 20000);

<quote>
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));
</quote>

Using 'Range in the Sort function properly handles both of these cases 
(and any other slices you need to use).  Take a look at the latter part 
of his posting again to see how this works.

-- Marc A. Criley
-- McKae Technologies
-- www.mckae.com
-- DTraq - XPath In Ada - XML EZ Out



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Arrays & pointers question
  2005-11-16 13:37         ` Marc A. Criley
@ 2005-11-16 18:37           ` csaagpDIESPAMDIE
  0 siblings, 0 replies; 7+ messages in thread
From: csaagpDIESPAMDIE @ 2005-11-16 18:37 UTC (permalink / raw)


Gotcha, thanks for that.




^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2005-11-16 18:37 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-15 19:28 Arrays & pointers question csaagpDIESPAMDIE
2005-11-15 19:53 ` tmoran
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

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