From: Jeffrey R. Carter on
Hibou57 (Yannick Duch�ne) wrote:
> On 29 oct, 22:36, "Jeffrey R. Carter" <spam.jrcarter....(a)spam.acm.org>
> wrote:
>> You're correct. But first, a probable error:
> He is not at all : his implementation is returning an access to String
> while the function is expected to return a String.

"You're correct" referred to "I think that my code returns the pointer to the
string and not the string."

--
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
From: Robert A Duff on
In addition to the good answers in this thread, another thing that might
work for the OP is:

Events_Type'Image(Event)

although it annoyingly uses ALL_CAPS, and doesn't work if
the event names contain blanks and whatnot.

"Hibou57 (Yannick Duch�ne)" <yannick_duchene(a)yahoo.fr> writes:

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

This part is not necessarily true. The compiler is free to do static
global allocation here.

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

The above strings are certainly statically allocated in GNAT.

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

I think the above array is, too, but I'm not sure.
You'd have to experiment to find out.

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

In other words, a good compiler will do the following tranformation for
you. The advantage is to avoid polluting the global namespace.

> 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

- Bob
From: Peter Mueller on
Hello,

On 29 Okt., 22:38, Hibou57 (Yannick Duchêne)
<yannick_duch...(a)yahoo.fr> wrote:
> On 29 oct, 21:48, Peter Mueller <peter.o.muel...(a)gmx.de> wrote:
>
>
>

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

So far I have not used ".all".

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

Ok

>
> 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 ?

Yes, this is clear.

>
> 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.
>
....
>
> What tutorial do you have in hands to learn Ada ?

I followed the intro from http://burks.bton.ac.uk/burks/language/ada/adacraft

Thanks for your comprehensive answer!

Peter
From: Hibou57 (Yannick Duchêne) on
On 30 oct, 10:10, Peter Mueller <peter.o.muel...(a)gmx.de> wrote:
> I followed the intro fromhttp://burks.bton.ac.uk/burks/language/ada/adacraft
Ah, the famous Craft. I've never read it (I've simply started learning
Ada with the ARM, then I looked at some document for some specific
stuff, like design and conventions), but I know it is very famous all
over the web. It just seems it is not updated to match Ada 2005 and
seems to be tied to Ada 95, but seems to be good if I believe what
people says about this one.

You may have a look at Ada-Log as well if you can : http://www.adalog.fr/
(but french only)

>
> Thanks for your comprehensive answer!
>
> Peter
You're welcome
(it appears to me I've left a lot of spelling mistakes in the prior
reply, sorry for that)