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: g2news2.google.com!news4.google.com!feeder3.cambriumusenet.nl!feed.tweaknews.nl!193.201.147.71.MISMATCH!xlned.com!feeder3.xlned.com!feeder.erje.net!news.tornevall.net!not-for-mail From: "Jeffrey R. Carter" Newsgroups: comp.lang.ada Subject: Re: Question on array of strings Date: Thu, 29 Oct 2009 14:36:31 -0700 Organization: TornevallNET - http://news.tornevall.net Message-ID: References: NNTP-Posting-Host: 212ab8b7aecf4a8f24a92269a9576aa5 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: 837d87fc61d5f14de0aa7e04cd2dec9a X-Complaints-To: abuse@tornevall.net X-Complaints-Language: Spoken language is english or swedish - NOT ITALIAN, FRENCH, GERMAN OR ANY OTHER LANGUAGE! In-Reply-To: X-Validate-Post: http://news.tornevall.net/validate.php?trace=837d87fc61d5f14de0aa7e04cd2dec9a X-SpeedUI: 1738 X-Complaints-Italiano: Parlo la lingua non � italiano User-Agent: Thunderbird 2.0.0.23 (Windows/20090812) X-Posting-User: 9b22bfe2855937f9b3faeec7cfc91295 Xref: g2news2.google.com comp.lang.ada:8855 Date: 2009-10-29T14:36:31-07:00 List-Id: Peter Mueller wrote: > > I have some strings and depending of an enum I want to return a > string. My code looks the following: > > function Event_To_String(Event : in Events_Type) return String is > Ev1_Name : aliased constant String :="ev3434"; > Ev2_Name : aliased constant String :="evEnd"; > eventStrings : constant array (1..2) of access constant String := > (Ev1_Name'Access,Ev2_Name'Access); > > begin > > return EventStrings(Events_Type'Pos(Event)); > end Event_To_String; > > The compiler says: > > expected type "Standard.String" > found type access to "Standard.String" defined ... > > I think that my code returns the pointer to the string and not the > string. But I don't know how to fix it. You're correct. But first, a probable error: 'Pos for an enumeration type returns values starting at zero: type E is (One, Two); E'Pos (One) = 0 E'Pos (Two) = 1 Your array index range is "Integer range 1 .. 2". So, assuming Events_Type is the first-named subtype (the name used in the type declaration), if you ever called this with Events_Type'First, you would get Constraint_Error. However, there is no reason to use Integer indices. An array may be indexed by any discrete subtype: type Event_Name is array (Events_Type) of ...; Then you can say Eventstrings (Event) As for your question, you dereference an access value by appending .all to it: Eventstrings (Event).all > Is there a better solution to create an array of strings and accessing > them with an index? For a discrete subtype like this, probably not. What you have is a mapping Events_Type => String. If you were mapping something non-discrete to String, than a map or similar associative data structure would be better. It's also possible to store String values in [Un]Bounded_String objects; that would be better if the strings are not constant. It would also eliminate (explicit) use of access types and values. -- Jeff Carter "C's solution to this [variable-sized array parameters] has real problems, and people who are complaining about safety definitely have a point." Dennis Ritchie 25