From: Ina Drescher on
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 ?
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);




From: Ludovic Brenta on
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.
From: sjw on
On Feb 24, 9:11 pm, Ina Drescher <ina.drescher.1...(a)googlemail.com>
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 ?
> 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);

You might declare the array type first:

type Integer_Array is array (Integer range <>) of Integer;

Arr : Integer_Array := (1 => 1, 2 => 2);

(if you don't do this, you'll have trouble passing Arr to
subprograms).

You can also write

Arr : Integer_Array := (1, 2);

but then the indices of Arr are -2147483648 .. -2147483647 (ie, start
at Integer'First. Not sure if this is mandated behaviour -- probably
-- or just the way GNAT works ...)

From: Randy Brukardt on
"Jeffrey R. Carter" <spam.jrcarter.not(a)spam.acm.org> wrote in message
news:hm4732$7f6$1(a)tornado.tornevall.net...
> sjw wrote:
>>
>> but then the indices of Arr are -2147483648 .. -2147483647 (ie, start
>> at Integer'First. Not sure if this is mandated behaviour -- probably
>
> The indices start at Integer'First. The exact value of Integer'First
> depends on the compiler. You cannot count on the range of Integer being
> greater than a 16-bit signed integer.
>
> This is mandated: ARM 4.3.3(26): "[The bounds of the index range of an
> array_aggregate are determined as follows:] For a
> positional_array_aggregate ..., the lower bound is ... that of the
> corresponding index subtype ..."

Right. There is a reason that String is defined:

array (Positive range <>) of Character;

Randy.


From: Robert A Duff on
"Jeffrey R. Carter" <spam.jrcarter.not(a)spam.acm.org> writes:

> sjw wrote:
>> but then the indices of Arr are -2147483648 .. -2147483647 (ie, start
>> at Integer'First. Not sure if this is mandated behaviour -- probably
>
> The indices start at Integer'First.

Right. And you might want to create an empty array that goes
from 'First to 'First-1, which causes trouble with Integer.
It's usually better to use "Positive range <>",
or sometimes "Natural range <>", rather than
"Integer range <>".

- Bob