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,efac8f93bb1bec0a X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!wns13feed!worldnet.att.net!attbi_s21.POSTED!53ab2750!not-for-mail From: "Jeffrey R. Carter" User-Agent: Thunderbird 1.5.0.9 (Windows/20061207) MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Interfacing with C - pointer problem References: <1170167017.790966.225850@v45g2000cwv.googlegroups.com> <1170267048.731810.18900@k78g2000cwa.googlegroups.com> In-Reply-To: <1170267048.731810.18900@k78g2000cwa.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: NNTP-Posting-Host: 12.201.97.213 X-Complaints-To: abuse@mchsi.com X-Trace: attbi_s21 1170287607 12.201.97.213 (Wed, 31 Jan 2007 23:53:27 GMT) NNTP-Posting-Date: Wed, 31 Jan 2007 23:53:27 GMT Organization: AT&T ASP.att.net Date: Wed, 31 Jan 2007 23:53:27 GMT Xref: g2news2.google.com comp.lang.ada:8799 Date: 2007-01-31T23:53:27+00:00 List-Id: Adam Beneschan wrote: >> type LPSTR is access Interfaces.C.Char; >> procedure C_Func (x : LPSTR); -- expects an address (C-pointer, 32-bit) >> and then >> data : Interfaces.C.char_array := Interfaces.C.To_C("ABC"); >> and >> C_Func (data(data'first)'unchecked_access); > > Or perhaps better: > > procedure C_Func (x : Interfaces.C.Strings.chars_ptr); > > data : aliased Interfaces.C.char_array := Interfaces.C.To_C > ("ABC"); > > C_Func (Interfaces.C.Strings.To_Chars_Ptr (data'unchecked_access)); > > May as well use the stuff Ada provides for this exact purpose. Right. Therefore, even better: procedure C_Func (X : in Interfaces.C.Char_Array); pragma Import (C, C_Func, ...); Data : Interfaces.C.Char_Array := Interfaces.C.To_C ("ABC); C_Func (X => Data); The language passes this a a pointer to Data (Data'First). See ARM B.3 (70): "An Ada parameter of an array type with component type T, of any mode, is passed as a t* argument to a C function, where t is the C type corresponding to the Ada type T." -- Jeff Carter "Alms for an ex-leper!" Monty Python's Life of Brian 75