From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!i39g2000yqm.googlegroups.com!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: Initialization of Arrays in Ada Date: Wed, 24 Feb 2010 13:30:21 -0800 (PST) Organization: http://groups.google.com Message-ID: <4e2ec300-eb01-4c64-93df-c599c351a580@i39g2000yqm.googlegroups.com> References: NNTP-Posting-Host: 94.108.241.180 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: posting.google.com 1267047021 19549 127.0.0.1 (24 Feb 2010 21:30:21 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 24 Feb 2010 21:30:21 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: i39g2000yqm.googlegroups.com; posting-host=94.108.241.180; posting-account=pcLQNgkAAAD9TrXkhkIgiY6-MDtJjIlC User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.6) Gecko/20091216 Iceweasel/3.5.6 (like Firefox/3.5.6; Debian-3.5.6-2),gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:9307 Date: 2010-02-24T13:30:21-08:00 List-Id: 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.