From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,6b3a3c920575b35a X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!news.maxwell.syr.edu!news-rtr.nyroc.rr.com!news-out.nyroc.rr.com!twister.nyroc.rr.com.POSTED!53ab2750!not-for-mail From: "REH" Newsgroups: comp.lang.ada References: <1154084819.088112.325730@p79g2000cwp.googlegroups.com> Subject: Re: How to pass two dimensional arrays to C X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2869 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2869 Message-ID: Date: Sat, 29 Jul 2006 05:47:52 GMT NNTP-Posting-Host: 69.205.134.80 X-Complaints-To: abuse@rr.com X-Trace: twister.nyroc.rr.com 1154152072 69.205.134.80 (Sat, 29 Jul 2006 01:47:52 EDT) NNTP-Posting-Date: Sat, 29 Jul 2006 01:47:52 EDT Organization: Road Runner Xref: g2news2.google.com comp.lang.ada:6009 Date: 2006-07-29T05:47:52+00:00 List-Id: "Jerry" wrote in message news:1154084819.088112.325730@p79g2000cwp.googlegroups.com... > 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); > > > > The Ada > > > type Long_Float_Array_1D is array (Integer range <>) of Long_Float; > type Long_Float_Array_2D is array (Integer range <>, integer range <>) > of Long_Float; > > subtype PLINT is Integer; > > subtype PL_Float_Array is Long_Float_Array_1D; > subtype PL_Float_Array_2D is Long_Float_Array_2D; > > procedure plmesh( > x : PL_Float_Array; > y : PL_Float_Array; > z : PL_Float_Array_2D; > nx : PLINT; > ny : PLINT; > opt : PLINT); > pragma Import(C, plmesh, "c_plmesh"); > > > > And the Ugly > > > AdaPLplotProject has exited due to signal 11 (SIGSEGV). > > This appears during the call to plmesh. Any ideas? > > Thanks, > Jerry > That's because C is not expecting to see a 2d array, but an array of pointers (jagged array). If C were looking for an actual 2d array of contiguous memory, you would see something like: void foo(int A[][10]); or even: void foo(int (*A)[10]); What you need to do is give it an array of access types to arrays. Something like: type D2 is array (1 .. 10) of Interface.C.double; pragma Convention(C, D2); type PD2 is access all D2; pragma Convention(C, PD2); type D2Arrays is array (1 .. 3) of aliased D2; pragma Convention(C, D2Arrays); type D1 is array (1 .. 3) of PD2; pragma Convention(C, D1); T : D2Arrays; Z : D1 := (1 => T(1)'Access, 2 => T(2)'Access, 3 => T(3)'Access) ; Z should now be usable in your function for PLFLT ** Note, that PLFLT ** alone doesn't tell you what C really wants. I had to go on the internet and search for your function to find out. It could have also meant the function was expecting a pointer to a pointer (a pointer by reference, so a pointer can be "returned"). It could also have meant that the function was going to allocate and "return" a 1d array. It could also have been an actual 2d array (with unknown dimensions). REH