comp.lang.ada
 help / color / mirror / Atom feed
From: "Hibou57 (Yannick Duchêne)" <yannick_duchene@yahoo.fr>
Subject: Re: Question on array of strings
Date: Thu, 29 Oct 2009 14:38:12 -0700 (PDT)
Date: 2009-10-29T14:38:12-07:00	[thread overview]
Message-ID: <562b9c59-5c4e-42d3-b4b9-bfcd33bc3211@j4g2000yqe.googlegroups.com> (raw)
In-Reply-To: b9669986-5b06-4805-9ce3-404b9af7f666@k4g2000yqb.googlegroups.com

On 29 oct, 21:48, Peter Mueller <peter.o.muel...@gmx.de> wrote:
> Hello,
>
> 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.
> Is there a better solution to create an array of strings and accessing
> them with an index?
>
> Thanks,
> Peter

The compiler error you've reported is not the single point to notice
about in test case.

The first one, the one you are talking about :
You have a function whose return type is String.
You have an array of anonymous access to String.
The return statement returns an item of that array.

So fat, you have then a function which is expected to return a String,
but you return an access to String.
The expected returned type of the function and the type you return are
not the same.
The compiler complains about that, it tries to tell you that the
expression you attempt to return, is not of type String.

To get a String from an access to String, you have to use the “ .all ”
suffixe.
Do know what is  “ .all ” ?
If you need explanations about it, just tell, you will be asked about
it.

Just change
“ return EventStrings(Events_Type'Pos(Event));  ”
into
“ return EventStrings(Events_Type'Pos(Event)).all;  ” and every thing
will be OK.

That's to correct what does not work.

To got further now : you wants an association from Events_Type to
String, aren't you ?
So why to you use an array whose index range is “ 1 .. 2 ” ?
I suggest you to use Events_Type instead.

So, as a second step, you may turn
“ eventStrings : constant array (1..2) of access constant String ”
into
“ eventStrings : constant array (Events_Type) of access constant
String ”

Do you understand what it is ?

Then, as you use Events_Type as the array index range, you do not need
any more (and must not also), the “ Events_Type'Pos(Event) ” and you
can use just Event instead

So then replace
“ return EventStrings(Events_Type'Pos(Event)).all ”
with
“ return EventStrings(Event).all ”

Providing that your Events_Type is a enumeration of two elements
(otherwise, just update the array initialization to match the required
number of items).

Next now : you have an array of constant, which will be initialized
each times you enter the function. There are no kind of “ static ” in
Ada like there are in C. To achieve static global allocation, you have
to declare your array outside of the function, at the package level.

So now, try to change

   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 (Events_Type) of access constant
String :=
        (Ev1_Name'Access,
         Ev2_Name'Access);

      begin
         return EventStrings(Event).all;
      end Event_To_String;begin

into

   Ev1_Name : aliased constant String :="ev3434";
   Ev2_Name : aliased constant String :="evEnd";

   eventStrings : constant array (Events_Type) of access constant
String :=
     (Ev1_Name'Access,
      Ev2_Name'Access);

   function Event_To_String(Event : in Events_Type) return String is
      begin
         return EventStrings(Event).all;
      end Event_To_String;begin

What tutorial do you have in hands to learn Ada ?

Read you soon

Y.



  parent reply	other threads:[~2009-10-29 21:38 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
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) [this message]
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