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-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,b16d5240727960c4 X-Google-Attributes: gid103376,public Path: controlnews3.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!colt.net!kibo.news.demon.net!news.demon.co.uk!demon!not-for-mail From: Simon Wright Newsgroups: comp.lang.ada Subject: Re: C bindings, Interfaces.C.Pointers etc. Date: 12 May 2004 05:50:03 +0100 Organization: Pushface Sender: simon@smaug.pushface.org Message-ID: References: NNTP-Posting-Host: pogner.demon.co.uk Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: news.demon.co.uk 1084337674 17307 62.49.19.209 (12 May 2004 04:54:34 GMT) X-Complaints-To: abuse@demon.net NNTP-Posting-Date: Wed, 12 May 2004 04:54:34 +0000 (UTC) User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.1 Xref: controlnews3.google.com comp.lang.ada:462 Date: 2004-05-12T05:50:03+01:00 List-Id: Jeffrey Carter writes: > Dr. Adrian Wrigley wrote: > > > Hi folks! > > I have a problem creating a binding to a C library. > > The library makes extensive use of arrays of arrays of structs. > > Both levels of arrays are of variable length. > > struct node { int index; double value; }; > > struct problem { node **x; }; > > When I attempt to define Ada types for this type of thing, > > I find I can't use access types to the arrays with the > > necessary C convention. > > "warning: this access type does not correspond to C pointer" > > is the compiler error. > > -- struct node { int index; double value; }; > type Node_Info is record > Index : Interfaces.C.Int; > Value : Interfaces.C.Double; > end record; > pragma Convention (C, Node_Info); > > -- struct problem { node **x; }; > type Node_Ptr is access all Node_Info; > pragma Convention (C, Node_Ptr); > type Node_Ptr_Ptr is access all Node_Ptr; > pragma Convention (C, Node_Ptr_Ptr); > type Problem_Info is record > X : Node_Ptr_Ptr; > end record; > pragma Convention (C, Problem_Info); > > This compiles fine. This is an exact duplicate of the C > declarations. But not, I think, of the implications. Where is the equivalent of problem p; node *np = *p.x; np++; // <<<< I've used constrained subtypes in the past: type Node_Array is array (Integer range <>) of Node_Info; then in a subprogram taking a Node_Array parameter Nodes, subtype Specific_Node_Array is Node_Array (Nodes'First .. Nodes'Last); type Node_Array_Access is access Specific_Node_Array; pragma Convention (C, Node_Array_Access); then declare a local binding to the C operation. Same again for Node_Ptr_Array, though the Ada side of this is going to be rather difficult since the Node_Ptrs in this are really Node_Array_Accesses and the Node_Arrays are not all of the same length. Perhaps the simplest way would be to use System.Address: (Arr (Arr'First))'Address > To deal with this using a higher level Ada > abstraction, you'd wrap something around this to provide that > abstraction. -- Simon Wright 100% Ada, no bugs.