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=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,6ec0e822a8924768 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 1994-10-18 07:20:14 PST Path: bga.com!news.sprintlink.net!howland.reston.ans.net!europa.eng.gtefsd.com!uhog.mit.edu!news.kei.com!eff!blanket.mitre.org!linus.mitre.org!linus!mbunix!emery From: emery@goldfinger.mitre.org (David Emery) Newsgroups: comp.lang.ada Subject: Re: C-Ada Import of struct's -- Help Date: 18 Oct 94 09:48:47 Organization: The Mitre Corp., Bedford, MA. Message-ID: References: NNTP-Posting-Host: goldfinger.mitre.org In-reply-to: mmcnett@sparc53.cs.uiuc.edu's message of 18 Oct 94 03:34:16 GMT Date: 1994-10-18T09:48:47+00:00 List-Id: Given struct queue_noncyc_s { char *debugname; /* name for debugging purposes */ int length; /* length (max items+1) of queue */ int first, last; /* begin and end of queue */ Qitem *entries; /* array[length] of entries */ }; I come up with the following Ada data structure with System; package C_Types is type char_star is new System.address; type c_int is new integer; end C_Types; type queue_noncyc_s is record debugname : C_Types.char_star; length : C_Types.c_int; first, last : C_Types.c_int; entries : System.address; -- handle specially end record; (There are 'standard tricks' for handling char * values. See my paper in Tri-Ada '90.) Now one problem is determining -how- the record is passed to C. Some C compilers support passing structs (required by ANSI C, I believe): int pass_struct (struct rec bar); others do not, and you pass the address of the struct (Original K&R C): int pass_addr (struct rec *bar); The latter case is more portable, and easier to represent. So, given our Ada type that we want to pass to C... q_rec : queue_noncyc_s; To pass it to pass_addr() we have: function pass_addr (addr : system.address) return c_types.int; ... pass_addr (q_rec'address); -- pretty straightforward A problem occurs with pass_struct(). I've seen cases where the C compiler passes the structure, and other cases where it passes the address of the structure. And I've seen Ada compilers that did not support the C compiler's choice. You'll either have to determine how your compiler does this, or, better, write some wrapper code in C to reduce the problem to the 'known case'. Write this C code, then call it from Ada. int pass_addr (bar) struct rec *bar; { return pass_struct (*bar); /* calls original routine */ }; The Ada call for this is the as shown above. dave -- --The preceeding opinions do not necessarily reflect the opinions of --The MITRE Corporation or its sponsors. -- "A good plan violently executed -NOW- is better than a perfect plan -- next week" George Patton -- "Any damn fool can write a plan. It's the execution that gets you -- all screwed up" James Hollingsworth -------------------------------------------------------------------------