From: "Jeffrey R. Carter" <spam.jrcarter.not@spam.acm.org>
Subject: Re: Question on array of strings
Date: Thu, 29 Oct 2009 14:36:31 -0700
Date: 2009-10-29T14:36:31-07:00 [thread overview]
Message-ID: <hcd1sd$fg0$1@news.tornevall.net> (raw)
In-Reply-To: <b9669986-5b06-4805-9ce3-404b9af7f666@k4g2000yqb.googlegroups.com>
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
next prev parent reply other threads:[~2009-10-29 21:36 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-10-29 20:48 Question on array of strings Peter Mueller
2009-10-29 21:36 ` Jeffrey R. Carter [this message]
2009-10-29 21:42 ` Hibou57 (Yannick Duchêne)
2009-10-29 22:58 ` Jeffrey R. Carter
2009-10-29 21:38 ` Hibou57 (Yannick Duchêne)
2009-10-30 0:12 ` Robert A Duff
2009-10-30 9:10 ` Peter Mueller
2009-10-30 10:14 ` Hibou57 (Yannick Duchêne)
2009-10-29 22:46 ` Gautier write-only
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox