comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: Initialization of Arrays in Ada
Date: Wed, 24 Feb 2010 13:30:21 -0800 (PST)
Date: 2010-02-24T13:30:21-08:00	[thread overview]
Message-ID: <4e2ec300-eb01-4c64-93df-c599c351a580@i39g2000yqm.googlegroups.com> (raw)
In-Reply-To: d7d8271e-9b42-4c77-ab26-83c56c481606@f8g2000yqn.googlegroups.com

Ina Drescher wrote:
> Hi Ada Users,
>
> I have a question concerning the initialization of an array in Ada.
>
> For instance I have sth. like this:
>
> arr : Array (Integer Range<>) of Integer:=(1=>1, 2=>2);
>
> I just want to leave out the 1=> 2=> ...
>
> Is there any way like declating undefinite range arrays that ada does
> the counting work:
>
> In C in know this would be like this:
>
> char name[]="Test" and the compiler counts the number of characters.
>
> How is this done in Ada ?

Name : String := "Test";

> I know it's possible to count the number of entries for myself and
> then I could eliminate the 1=> ... so that it will look like this:
>
> arr : Array (Integer Range 1..2) of Integer:=(1, 2);

This works, and the following works too:

Arr : array (Positive range <>) := (1, 2);

But you should not create anonymous array types like that because that
would prevent you from passing Arr as a parameter to a subprogram.
Instead, you should declare a named type like so:

type A is array (Positive range <>) of Integer; -- named,
unconstrained type
My_Array : A := (1, 2); -- array constrained by its initial value

After that, My_Array is constrained; you cannot change its bounds
anymore; you can only change its contents:

My_Array := (3, 4); -- OK
My_Array := (5, 6, 7); -- Error, My_Array has only 2 components

HTH

--
Ludovic Brenta.



  reply	other threads:[~2010-02-24 21:30 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-02-24 21:11 Initialization of Arrays in Ada Ina Drescher
2010-02-24 21:30 ` Ludovic Brenta [this message]
2010-02-24 21:30 ` sjw
2010-02-24 21:52   ` Jeffrey R. Carter
2010-02-24 23:50     ` Randy Brukardt
2010-02-25  0:01     ` Robert A Duff
replies disabled

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