comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: Calling Ada Procedure from C - how to handle a char ptr/array
Date: Fri, 18 Nov 2011 12:56:08 -0700
Date: 2011-11-18T12:56:08-07:00	[thread overview]
Message-ID: <ja6dc0$qj7$1@tornado.tornevall.net> (raw)
In-Reply-To: <8e5541a3-cdd5-40af-a6fd-f7539ee61326@l19g2000yqc.googlegroups.com>

On 11/18/2011 12:40 PM, awdorrin wrote:
> What would be the recommended way to pass a C char pointer or char
> array into an Ada function or procedure that will be called from a C
> program?
>
> After reading through the Interfaces.C.* packages, I'm assuming I
> could do something like this:
>
> In my Ada procedure:
>
> procedure Get_Text( s : out Interfaces.C.char_array) is
>    Label : String(1..8)
> begin
>    Label(1..8) == "12345678";

Someone's been writing C.

>    S := Interfaces.C.To_C(Label);
>   end;
 >
 > however when I compile the code I get the following warning:
 >    warning: type of argument "Get_Text.s" is unconstrained array
 >    warning: foreign caller must pass bounds explicitly

The problem here is that Ada doesn't know the bounds of S. C has to also pass 
that information.

 > Then when I run, I get an error from the following line of code: S :=
 > Interfaces.C.To_C(Label);
 > "raised CONSTRAINT_ERROR : length check failed.
 >
 > What length check?

An assignment to S must have a right-hand side that's the same length as S. I 
suspect you'll have to assign to a slice:

S (S'First .. S'First + Label'Length) := To_C (Label);

> Is there a better way to be doing this?

A function that returns a Char_Array might be better than an out parameter.

-- 
Jeff Carter
"Go and boil your bottoms."
Monty Python & the Holy Grail
01



  reply	other threads:[~2011-11-18 19:56 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-18 19:40 Calling Ada Procedure from C - how to handle a char ptr/array awdorrin
2011-11-18 19:56 ` Jeffrey Carter [this message]
2011-11-18 20:19   ` awdorrin
2011-11-18 20:41     ` awdorrin
2011-11-18 21:38       ` Adam Beneschan
2011-11-18 22:32       ` Georg Bauhaus
2011-11-19  2:16 ` Adam Beneschan
2011-11-21 16:33   ` awdorrin
2011-11-22 13:20     ` awdorrin
2011-11-22 13:28       ` Simon Wright
replies disabled

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