comp.lang.ada
 help / color / mirror / Atom feed
From: "Mark Lundquist" <mlundquist2@attbi.com>
Subject: Re: Unconstrained arrays
Date: Tue, 11 Dec 2001 18:24:17 GMT
Date: 2001-12-11T18:24:17+00:00	[thread overview]
Message-ID: <lbsR7.8159$7y.74087@rwcrnsc54> (raw)
In-Reply-To: 3C163FBE.9CB75916@rz.uni-karlsruhe.de


"Michael Unverzagt" <uhcm@rz.uni-karlsruhe.de> wrote in message
news:3C163FBE.9CB75916@rz.uni-karlsruhe.de...
> Hi!
>
> We have to write a programm that uses an unconstrained array of
> ustrings, where the size of the array should be determined by user
> input...
>
> How does this work? Someone told me, I have to use subtypes but...
> Does anyone know a simple example where I can learn how I can define the
> size of an array at runtime by user input?

Hi Mike,

The first thing to understand is that in Ada, the bounds of an array need
not be static (they are constant, but need not be static).  So they can be
determined by a variable, a non-static constant, or some other non-static
expression like a function call, and the array will likely be allocated from
the stack, not the heap, and in any case you don't need any pointers or
explicit heap allocation.  So, two ways to do it:

Way 1:

procedure Main_Program is
    .
    .
    Array_Size : Natural;
    .
    .
begin
    .
    . -- Get input from the user
    . -- into the variable Array_Size
    .
    declare
        My_Array : array (1 .. Array_Size) of Foo;
    begin
        .
        .    -- do your thing...


Way 2:

procedure Main_Program is
    .
    .
    function Array_Size return Natural is
        Size_From_User : Natural;
    begin
        . - get input from user
        .
        .
        return Size_From_User ;
    end Array_Size;

    My_Array (1 .. Array_Size) of Foo;

begin
    .
    .
    .


Have fun,
-- Mark






  parent reply	other threads:[~2001-12-11 18:24 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-12-11 17:17 Unconstrained arrays Michael Unverzagt
2001-12-11 18:22 ` Stephen Leake
2001-12-11 18:24 ` Mark Lundquist [this message]
  -- strict thread matches above, loose matches on Subject: below --
2009-03-17  0:59 Unconstrained Arrays belteshazzar
2009-03-17  1:49 ` Jeffrey R. Carter
2009-03-17  2:58   ` belteshazzar
2009-03-17  4:15     ` Jeffrey Creem
2009-03-17  5:20     ` Jeffrey R. Carter
2009-03-17 17:56       ` Jeffrey R. Carter
2009-03-17 23:10         ` belteshazzar
2009-03-18 18:31           ` Jeffrey R. Carter
2009-03-20  1:53           ` Peter C. Chapin
2009-03-20  6:45             ` sjw
2009-03-20  9:46               ` Jacob Sparre Andersen
2009-03-20 11:40               ` Jean-Pierre Rosen
2009-03-25 21:11                 ` sjw
2009-03-25 22:30                   ` Robert A Duff
2009-03-25 23:28                     ` Randy Brukardt
2009-03-26  0:03                       ` Jeffrey R. Carter
2009-03-26  1:00                         ` Robert A Duff
2009-03-20 12:15               ` christoph.grein
2009-03-20 15:45               ` Adam Beneschan
2009-03-23  8:26                 ` belteshazzar
2009-03-25 21:21                 ` sjw
2009-03-25 22:03                   ` Adam Beneschan
2009-03-26  1:32                     ` tmoran
2009-03-27  8:39                   ` Jean-Pierre Rosen
2009-03-27 20:07                     ` sjw
2009-03-29 16:24                     ` sjw
2009-03-27 11:57                   ` Gautier
2009-03-17 15:33     ` Adam Beneschan
2009-03-17 23:00       ` belteshazzar
2009-03-17 20:14 ` anon
1993-08-15  5:01 Unconstrained arrays Alex Blakemore
1993-08-13 21:08 J. Craig Heberle
1993-08-13 12:34 Paul Durbin
1993-08-12 21:23 Robert Dewar
1993-08-12 19:25 Wes Groleau x1240 C73-8
1993-08-12 17:27 agate!howland.reston.ans.net!math.ohio-state.edu!magnus.acs.ohio-state.ed
1993-08-12 16:26 Mark A Biggar
1993-08-12 16:00 Dave Collar d x7468
1993-08-12 15:28 Robert I. Eachus
1993-08-12 15:00 Robert Dewar
1993-08-12 13:03 Raymond Blaak
1993-08-12 12:14 cis.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!howland.
1993-08-12 12:03 cis.ohio-state.edu!pacific.mps.ohio-state.edu!math.ohio-state.edu!magnus.
1993-08-11 23:42 Kenneth Anderson
1993-08-11 23:40 cis.ohio-state.edu!math.ohio-state.edu!cs.utexas.edu!swrinde!menudo.uh.ed
1993-08-11 22:29 Kenneth Anderson
replies disabled

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