comp.lang.ada
 help / color / mirror / Atom feed
From: "Adam Beneschan" <adam@irvine.com>
Subject: Re: How to pass two dimensional arrays to C
Date: 28 Jul 2006 09:53:53 -0700
Date: 2006-07-28T09:53:53-07:00	[thread overview]
Message-ID: <1154105633.391460.171610@i42g2000cwa.googlegroups.com> (raw)
In-Reply-To: 1154084819.088112.325730@p79g2000cwp.googlegroups.com

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. 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);

z is not a two-dimensional array; it's an array of pointers.

Can I guess that you're not all that familiar with C?  * means pointer,
so an object of type PLFLT* is a pointer to a PLFLT.  It's common to
use this idiom to pass a one-dimensional array, because the machine
would pass the address of the array anyway (i.e. the address of the
leftmost element of the array), and you can use pointer arithmetic on
the parameters to get the other elements of the array.  (In fact, array
indexing in C is really just a shorthand for pointer arithmetic.)  But
one dimension is all you can do with this idiom; using two *'s doesn't
give you a two-dimensional array.  PLFLT** is a pointer to a pointer to
a PLFLT, which means that the pointer you pass as a parameter has to
point to a memory location that contains another pointer.  So to use
this routine, you'll need to construct an array of pointers somewhere
(shouldn't be too hard using 'Access, if your 2-D array has aliased
components, but don't take my word for this since I haven't tried it
using that compiler).

Hope this helps,
                              -- Adam




  parent reply	other threads:[~2006-07-28 16:53 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
2006-07-28 16:53 ` Adam Beneschan [this message]
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