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-Thread: 103376,e940dc253695e939 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!news1.google.com!postnews.google.com!b64g2000hsa.googlegroups.com!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: Pointer Date: Mon, 19 May 2008 05:09:46 -0700 (PDT) Organization: http://groups.google.com Message-ID: <17836d5d-179f-4867-a4cf-1caacdc6581f@b64g2000hsa.googlegroups.com> References: <2rjq6ys3fang$.1ir8q3892mjwo$.dlg@40tude.net> <483152DC.4040607@gmail.com> <93f38d4d-b34f-4858-a818-7dc5d65cbb0b@m36g2000hse.googlegroups.com> <4831652D.3020507@gmail.com> NNTP-Posting-Host: 153.98.68.197 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1211198986 28762 127.0.0.1 (19 May 2008 12:09:46 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 19 May 2008 12:09:46 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: b64g2000hsa.googlegroups.com; posting-host=153.98.68.197; posting-account=pcLQNgkAAAD9TrXkhkIgiY6-MDtJjIlC User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4.3) Gecko/20040924,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:236 Date: 2008-05-19T05:09:46-07:00 List-Id: S=E9bastien wrote: > char* get_nth(char** data, unsigned int n) { > return data[i]; > } > > Ada binding: > function get_nth(data: System.Address; n: Int) return Char_Ptr; > pragma Import(C, get_nth, "get_nth"); > > Then: > for i in Natural range 0 .. N loop > MyString :=3D Interfaces.C.Strings.Value(get_nth(CharPtrPtr, i)); > end loop; > > I know it's working, but I have to code a C function in order to do > that, is there a way to replace get_nth by an ada code function? > > Hope it's a better explanation ;-) OK, then how about: type Array_Of_C_Strings is array (Positive range <>) of Interfaces.C.Strings.chars_ptr; pragma Convention (C, Array_Of_C_Strings); type Array_Of_Strings is array (Positive range <>) of access String; function Get return Array_Of_C_Strings is function How_Many_Strings return Natural; pragma Import (C, How_Many_Strings, "MYCFunctionTellingMeHowManyStrings"); Length : constant Natural :=3D How_Many_Strings; function Get_Array return Array_Of_C_Strings; pragma Import (C, Get_Array, "MyCFunctionReturninCharStarStar"); Raw_Result : Array_Of_C_Strings (1 .. Length) :=3D Get_Array; Result : Array_Of_Strings (1 .. Length); begin for K in Result'Length loop Result (K) :=3D new String'(Interfaces.C.Strings.Value (Raw_Result (K))); end loop; return Result; end Get; This duplicates the strings and creates an array of access values; each access value remembers the length of the string it points to. There is probably another solution that avoids this duplication. -- Ludovic Brenta.