comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey R. Carter" <jrcarter@acm.org>
Subject: Re: Interfacing with C - pointer problem
Date: Wed, 31 Jan 2007 23:53:27 GMT
Date: 2007-01-31T23:53:27+00:00	[thread overview]
Message-ID: <XZ9wh.248687$aJ.60841@attbi_s21> (raw)
In-Reply-To: <1170267048.731810.18900@k78g2000cwa.googlegroups.com>

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



      reply	other threads:[~2007-01-31 23:53 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-01-30 14:23 Interfacing with C - pointer problem Gerd
2007-01-30 15:53 ` Frank J. Lhota
2007-01-30 21:10 ` tmoran
2007-01-31 18:10   ` Adam Beneschan
2007-01-31 23:53     ` Jeffrey R. Carter [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox