comp.lang.ada
 help / color / mirror / Atom feed
* Calling Ada Procedure from C - how to handle a char ptr/array
@ 2011-11-18 19:40 awdorrin
  2011-11-18 19:56 ` Jeffrey Carter
  2011-11-19  2:16 ` Adam Beneschan
  0 siblings, 2 replies; 10+ messages in thread
From: awdorrin @ 2011-11-18 19:40 UTC (permalink / raw)


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";
  S := Interfaces.C.To_C(Label);
 end;

with a C program that does the following:

int main() {
  char temp[8+1];
  memset( temp, '\0', 9);

  adainit();
  Get_Text(temp);
  printf("Temp is: %s\n", temp);
  adafinal();
}

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

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?

Is there a better way to be doing this?

Thanks!
-Al



^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2011-11-22 13:28 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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