comp.lang.ada
 help / color / mirror / Atom feed
From: convex!rtorres@uunet.uu.net  (Randy Torres)
Subject: Variant Records from Ada to C
Date: 1 Oct 92 14:23:09 GMT	[thread overview]
Message-ID: <rtorres.717949389@mikey> (raw)

Would the following be safe.  I have a variant record and want to pass
it to a C routine.  I currently know what the data structures will look
like when past to C.  However, will this data structure change with
releases of Ada compilers, as well as different compilers?


with system;

procedure a is
   procedure pass_string_array( str : system.address);
   pragma interface(c, pass_string_array);
   procedure pass_record( rec : system.address);
   pragma interface(c, pass_record);
   type xrec (p1 : integer; p2: integer) is
   record
      b  : integer;
      s1 : string(1..p1);
      c  : integer;
      s2 : string(1..p2);
      case p1 is
         when 7 =>
            fld1 : integer;
         when others =>
            fld2 : integer;
      end case;
   end record;
   myrec : xrec(7,8);
begin
   myrec.b := 66;
   myrec.c := 77;
   myrec.s1 := "AAAAABB";
   myrec.s2 := "CCCCCDDD";
   myrec.fld1 := 88;
   pass_string_array(myrec.s1'address);
   pass_string_array(myrec.s2'address);
   pass_record(myrec'address);
end;




C file.



#include <stdio.h>
   typedef struct xrec {      
      int p1;
      int p2;
      int b;
      unsigned s1_offset;
      struct {
         int element_size;    /* s1(1)'size */
         int lower;           /* s1'first */
         int upper;           /* s1'last */
         int total;           /* s1'size */
      } dv1;
      int c;
      unsigned s2_offset;
      struct {
         int element_size;    /* s2(1)'size */
         int lower;           /* s2'first */
         int upper;           /* s2'last */
         int total;           /* s2'size */
      } dv2;
      union {
         int fld1;
         int fld2;
      } n;
   } *xrec_t;

void pass_record(xrec_t my)
{
   char *s1, *s2;
   s1 = (char *)&(my->s1_offset);
   s1 += my->s1_offset;
   s2 = (char *)&(my->s2_offset);
   s2 += my->s2_offset;
   printf("p1 = %d\n",my->p1);
   printf("p2 = %d\n",my->p2);
   printf("b = %d\n",my->b);
   printf("c = %d\n",my->c);
   printf("fld1 = %d\n", my->n.fld1);
   printf("s1 = %s\n",s1);   
   printf("s2 = %s\n",s2);   
}

void pass_string_array(char *s)
{
   printf("s = %s\n",s);   
}




Sharon Shaw			
CONVEX Systems Integration and Support		

             reply	other threads:[~1992-10-01 14:23 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1992-10-01 14:23 Randy Torres [this message]
  -- strict thread matches above, loose matches on Subject: below --
1992-10-02 15:17 Variant Records from Ada to C WIS Program Group 
1992-10-02 19:01 agate!spool.mu.edu!sdd.hp.com!caen!deccrl!news.crl.dec.com!pa.dec.com!dat
replies disabled

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