comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: How to pass two dimensional arrays to C
Date: Fri, 28 Jul 2006 14:27:53 +0200
Date: 2006-07-28T14:27:57+02:00	[thread overview]
Message-ID: <e0j050mqvoys.8t63dvs0prws$.dlg@40tude.net> (raw)
In-Reply-To: 1154084819.088112.325730@p79g2000cwp.googlegroups.com

On 28 Jul 2006 04:06:59 -0700, Jerry wrote:

> In my project to write a binding for a popular plotter written in C,
> I've hit a brick wall and the problem might be associated with passing
> two-dimensionaal arrays to C.

Well, C does not have arrays (of either dimensionality), so you cannot pass
an Ada (true) array to C, as-is. You can pass a pointer to its first
element. 2D arrays in C are often emulated by using pointers to pointers.
This structure is even more distant from Ada's 2D array.

> I'm using GNAT on OS X and Xcode. I can
> pass one-dimensional arrays OK. Here are the pertinent parts from an
> example where the problem happens:
> 
>       The C
> 
> typedef double PLFLT;
> typedef int PLINT;
> 
> void c_plmesh(PLFLT *x, PLFLT *y, PLFLT **z, PLINT nx, PLINT ny, PLINT
> opt);

[...] 
> This appears during the call to plmesh. Any ideas? 

I'd suggest you to start with the Annex B.3. What you need is probably

with Interfaces.C;
with Interfaces.C.Pointers;

type PLFLT is new Interfaces.C.Double;
type PLINT is new Interfaces.C.Int;

type PLFLT_Array is array (PLINT range <>) of aliased PLFLT;
package PLFLT_Pointers is
   new Interfaces.C.Pointers
       (  Index              => PLINT,
          Element            => PLFLT,
          Element_Array      => PLFLT_Array,
          Default_Terminator => 0.0
       );
subtype PLFLT_Ptr is PLFLT_Pointers.Pointer;

type PLFLT_Ptr_Array is array (PLINT range <>) of aliased PLFLT_Ptr;
package PLFLT_Pointers_To_Pointers is
   new Interfaces.C.Pointers
       (  Index              => PLINT,
          Element            => PLFLT_Ptr,
          Element_Array      => PLFLT_Ptr_Array,
          Default_Terminator => null
       );
subtype PLFLT_Ptr_Ptr is PLFLT_Pointers_To_Pointers.Pointer;

procedure plmesh
          (  x   : PLFLT_Ptr;
             y   : PLFLT_Ptr;
             z   : PLFLT_Ptr_Ptr;
             nx  : PLINT;
             ny  : PLINT;
             opt : PLINT
          );
pragma Import(C, plmesh, "c_plmesh"); 

You might wish to write a wrapper to plmesh to be able to pass PLFLT_Array
and PLFLT_Ptr_Array down to it.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  parent reply	other threads:[~2006-07-28 12:27 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-07-28 11:06 How to pass two dimensional arrays to C Jerry
2006-07-28 12:27 ` Jeffrey Creem
2006-07-28 12:27 ` Dmitry A. Kazakov [this message]
2006-07-28 16:53 ` Adam Beneschan
2006-07-28 20:15 ` Jeffrey R. Carter
2006-07-28 20:46 ` Jerry
2006-07-28 21:14   ` Jeffrey Creem
2006-07-28 22:54   ` Björn Persson
2006-07-29  1:14     ` Jeffrey R. Carter
2006-07-29  7:11       ` Simon Wright
2006-07-29 22:12         ` Jeffrey R. Carter
2006-07-30 11:18           ` Simon Wright
2006-07-30 11:20           ` Simon Wright
2006-07-29  4:19   ` REH
2006-07-29  4:28     ` REH
2006-07-29  4:30       ` REH
2006-08-14  6:59     ` Dave Thompson
2006-07-29  5:47 ` REH
2006-08-02  8:20 ` Jerry
2006-08-02  9:03   ` Dmitry A. Kazakov
2006-08-02 10:22     ` Jerry
2006-08-02 18:25   ` Björn Persson
2006-08-05  1:03 ` Jerry
replies disabled

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