comp.lang.ada
 help / color / mirror / Atom feed
From: igor.kh@gmail.com
Subject: Re: Returning data from Ada to C
Date: 7 May 2005 15:12:07 -0700
Date: 2005-05-07T15:12:07-07:00	[thread overview]
Message-ID: <1115503927.228956.100050@z14g2000cwz.googlegroups.com> (raw)
In-Reply-To: hDBee.6345$GQ5.6288@newsread1.news.pas.earthlink.net

Jeffrey Carter wrote:
> Igor Khavkine wrote:

> > Great! Now, what type will `Solvec' have? I presume that GNAT will
> > bark at me if I use the wrong type.
>
> In Ada, it's a pointer to Double; to C it should look like a pointer
> to double. You probably should declare your array type to be
> convention C, just to be on the safe side.

I've tried to construct a simple example where this would work.
However, I am still failing. I can't get the types to match up. Below
are the files that I'm using. The exact error from gnat is:

hello_world.adb:18:19: expected type "Interfaces.C.Pointers.Pointer"
from instance at c_integer_arrays.ads:6

== c_integer_arrays.ads ================
with Interfaces.C; use Interfaces.C;
with Interfaces.C.Pointers;

package C_Integer_Arrays is
	type C_Integer_Array is array (size_t range <>) of aliased int;
	package C_IntArrs is
		new Interfaces.C.Pointers(size_t, int,
			C_Integer_Array, 0);
end C_Integer_Arrays;
========================================

== hello_world.ads =====================
with Interfaces.C; use Interfaces.C;
with C_Integer_Arrays; use C_Integer_Arrays;

procedure Hello_World ( i : in out integer; a_ptr : out
C_IntArrs.Pointer );
pragma Export (C, Hello_World);
========================================

== hello_world.adb =====================
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

with Interfaces.C; use Interfaces.C;
with C_Integer_Arrays; use C_Integer_Arrays;

procedure Hello_World ( i : in out integer;
	a_ptr : out C_IntArrs.Pointer ) is
	a : aliased array(0..2) of int;
	pragma Convention (C, a);
begin
	Put ("Hello world!");
	Put (i);
	i := 10;
	a(0) := 3;
	a(1) := 2;
	a(2) := 1;
	a_ptr := a'Unchecked_Access;
end Hello_World;
========================================

== hello.c =============================
#include <stdio.h>

void adainit();
void adafinal();
void hello_world(int *i, int **a_ptr);

int main()
{
	int i = 1;
	int *a_ptr;

	printf ("Calling Ada...\n");

	adainit();
	hello_world(&i, &a_ptr);

	printf ("...done\n");
	printf ("Returned i = %d\n", i);
	for (i=0; i<3; i++)
		printf ("a_ptr[%d] = %d\n", i, a_ptr[i]);

	adafinal();

	return 0;
}
========================================

What am I missing to make this work?

Thanks in advance.

Igor




  reply	other threads:[~2005-05-07 22:12 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-05-03 23:49 Returning data from Ada to C igor.kh
2005-05-04  4:49 ` Jeffrey Carter
2005-05-04 13:45   ` Igor Khavkine
2005-05-04 13:58     ` Alex R. Mosteo
2005-05-06  3:36     ` Jeffrey Carter
2005-05-07 22:12       ` igor.kh [this message]
2005-05-08  2:07         ` Jeffrey Carter
2005-05-08 19:58           ` Jeffrey Carter
2005-05-08  9:41         ` Martin Dowie
2005-05-08 14:55           ` Ludovic Brenta
2005-05-08 14:58             ` Ludovic Brenta
2005-05-09 22:49               ` igor.kh
2005-05-10 12:48                 ` Ludovic Brenta
2005-05-16  4:35   ` Dave Thompson
2005-05-16  5:03     ` Jeffrey Carter
replies disabled

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