comp.lang.ada
 help / color / mirror / Atom feed
* Initialization of Arrays in Ada
@ 2010-02-24 21:11 Ina Drescher
  2010-02-24 21:30 ` Ludovic Brenta
  2010-02-24 21:30 ` sjw
  0 siblings, 2 replies; 6+ messages in thread
From: Ina Drescher @ 2010-02-24 21:11 UTC (permalink / raw)
  Cc: wolfgang.meyerle

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







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

* Re: Initialization of Arrays in Ada
  2010-02-24 21:11 Initialization of Arrays in Ada Ina Drescher
@ 2010-02-24 21:30 ` Ludovic Brenta
  2010-02-24 21:30 ` sjw
  1 sibling, 0 replies; 6+ messages in thread
From: Ludovic Brenta @ 2010-02-24 21:30 UTC (permalink / raw)


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.



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

* Re: Initialization of Arrays in Ada
  2010-02-24 21:11 Initialization of Arrays in Ada Ina Drescher
  2010-02-24 21:30 ` Ludovic Brenta
@ 2010-02-24 21:30 ` sjw
  2010-02-24 21:52   ` Jeffrey R. Carter
  1 sibling, 1 reply; 6+ messages in thread
From: sjw @ 2010-02-24 21:30 UTC (permalink / raw)


On Feb 24, 9:11 pm, Ina Drescher <ina.drescher.1...@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 ...)




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

* Re: Initialization of Arrays in Ada
  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
  0 siblings, 2 replies; 6+ messages in thread
From: Jeffrey R. Carter @ 2010-02-24 21:52 UTC (permalink / raw)


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 ..."

-- 
Jeff Carter
"Don't knock masturbation. It's sex with someone I love."
Annie Hall
45



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

* Re: Initialization of Arrays in Ada
  2010-02-24 21:52   ` Jeffrey R. Carter
@ 2010-02-24 23:50     ` Randy Brukardt
  2010-02-25  0:01     ` Robert A Duff
  1 sibling, 0 replies; 6+ messages in thread
From: Randy Brukardt @ 2010-02-24 23:50 UTC (permalink / raw)


"Jeffrey R. Carter" <spam.jrcarter.not@spam.acm.org> wrote in message 
news:hm4732$7f6$1@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.





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

* Re: Initialization of Arrays in Ada
  2010-02-24 21:52   ` Jeffrey R. Carter
  2010-02-24 23:50     ` Randy Brukardt
@ 2010-02-25  0:01     ` Robert A Duff
  1 sibling, 0 replies; 6+ messages in thread
From: Robert A Duff @ 2010-02-25  0:01 UTC (permalink / raw)


"Jeffrey R. Carter" <spam.jrcarter.not@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



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

end of thread, other threads:[~2010-02-25  0:01 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-02-24 21:11 Initialization of Arrays in Ada Ina Drescher
2010-02-24 21:30 ` Ludovic Brenta
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

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