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,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,e61e0c2934b7f4d5 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,CP1252 Path: g2news2.google.com!postnews.google.com!j4g2000yqe.googlegroups.com!not-for-mail From: =?ISO-8859-1?Q?Hibou57_=28Yannick_Duch=EAne=29?= Newsgroups: comp.lang.ada Subject: Re: Question on array of strings Date: Thu, 29 Oct 2009 14:38:12 -0700 (PDT) Organization: http://groups.google.com Message-ID: <562b9c59-5c4e-42d3-b4b9-bfcd33bc3211@j4g2000yqe.googlegroups.com> References: NNTP-Posting-Host: 77.198.58.70 Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1256852293 12962 127.0.0.1 (29 Oct 2009 21:38:13 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 29 Oct 2009 21:38:13 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: j4g2000yqe.googlegroups.com; posting-host=77.198.58.70; posting-account=vrfdLAoAAAAauX_3XwyXEwXCWN3A1l8D User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; fr),gzip(gfe),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:8856 Date: 2009-10-29T14:38:12-07:00 List-Id: On 29 oct, 21:48, Peter Mueller 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 > =A0 =A0Ev1_Name : aliased constant String :=3D"ev3434"; > =A0 =A0Ev2_Name : aliased constant String :=3D"evEnd"; > =A0 =A0eventStrings : constant array (1..2) of access constant String := =3D > (Ev1_Name'Access,Ev2_Name'Access); > > =A0 =A0begin > > =A0 =A0return 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 =93 .all =94 suffixe. Do know what is =93 .all =94 ? If you need explanations about it, just tell, you will be asked about it. Just change =93 return EventStrings(Events_Type'Pos(Event)); =94 into =93 return EventStrings(Events_Type'Pos(Event)).all; =94 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 =93 1 .. 2 =94 ? I suggest you to use Events_Type instead. So, as a second step, you may turn =93 eventStrings : constant array (1..2) of access constant String =94 into =93 eventStrings : constant array (Events_Type) of access constant String =94 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 =93 Events_Type'Pos(Event) =94 and you can use just Event instead So then replace =93 return EventStrings(Events_Type'Pos(Event)).all =94 with =93 return EventStrings(Event).all =94 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 =93 static =94 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 :=3D"ev3434"; Ev2_Name : aliased constant String :=3D"evEnd"; eventStrings : constant array (Events_Type) of access constant String :=3D (Ev1_Name'Access, Ev2_Name'Access); begin return EventStrings(Event).all; end Event_To_String;begin into Ev1_Name : aliased constant String :=3D"ev3434"; Ev2_Name : aliased constant String :=3D"evEnd"; eventStrings : constant array (Events_Type) of access constant String :=3D (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.