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,d71460587da14d5b X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-07-30 16:50:27 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!arclight.uoregon.edu!wn13feed!wn12feed!worldnet.att.net!204.127.198.203!attbi_feed3!attbi.com!rwcrnsc51.ops.asp.att.net.POSTED!not-for-mail From: tmoran@acm.org Newsgroups: comp.lang.ada Subject: Re: Importing C structs? References: X-Newsreader: Tom's custom newsreader Message-ID: <6RYVa.16208$It4.10081@rwcrnsc51.ops.asp.att.net> NNTP-Posting-Host: 12.234.13.56 X-Complaints-To: abuse@comcast.net X-Trace: rwcrnsc51.ops.asp.att.net 1059609026 12.234.13.56 (Wed, 30 Jul 2003 23:50:26 GMT) NNTP-Posting-Date: Wed, 30 Jul 2003 23:50:26 GMT Organization: Comcast Online Date: Wed, 30 Jul 2003 23:50:26 GMT Xref: archiver1.google.com comp.lang.ada:41049 Date: 2003-07-30T23:50:26+00:00 List-Id: > type Jpeg_Error_Mgr is record > ... > Msg_Parm : System.Address; This is wrong. The C has > union { > int i[8]; > char s[JMSG_STR_PARM_MAX]; > } msg_parm; which means msg_parm is JMSG_STR_PARM_MAX number of bytes, interpretable sometimes as a char array and sometimes as an int array followed by filler. msg_parm is never an address. To match the C, I suggest making msg_parm an interfaces.c.char_array (the larger of the two objects), and then doing an unchecked_conversion on those (relatively rare) occasions when you need to treat it as an array of int's. Here's a compilable example: with interfaces.c; with ada.unchecked_conversion; procedure testluc is JMSG_STR_PARM_MAX: constant := 80; subtype Msg_Parm_C_Type is Interfaces.C.Char_Array(1 .. JMSG_STR_PARM_MAX); subtype I_Part is Interfaces.C.Size_T range 1 .. 32; subtype Msg_Parm_C_I_Part is Interfaces.C.Char_Array(I_Part); type Msg_Parm_I_Type is array(1 .. 8) of Interfaces.C.Int; function To_I is new Ada.Unchecked_Conversion (Source=>Msg_Parm_C_I_Part, Target=>Msg_Parm_I_Type); function To_C is new Ada.Unchecked_Conversion (Source=>Msg_Parm_I_Type, Target=>Msg_Parm_C_I_Part); type Jpeg_Error_Mgr is record msg_parm: Msg_Parm_C_Type; end record; X : Jpeg_Error_Mgr; X_I : Msg_Parm_I_Type; N : Interfaces.C.Int; begin -- fetch ints from X.msg_parm N := To_I(X.msg_parm(I_Part))(1); N := To_I(X.msg_parm(I_Part))(2); -- store ints into X.msg_parm X_I(1) := 1; X_I(8) := 2; X.msg_parm(I_Part):=To_C(X_I); end testluc;