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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,747d1273d7d099f,start X-Google-Attributes: gid103376,public From: Simon Wright Subject: Interfacing to C, output arrays Date: 1996/12/02 Message-ID: #1/1 X-Deja-AN: 201944241 x-nntp-posting-host: pogner.demon.co.uk sender: simon@pogner organization: At Home newsgroups: comp.lang.ada Date: 1996-12-02T00:00:00+00:00 List-Id: The code below shows an interface to a C function int accessor(int n, int* a); which fills the n-element array a with data. The code works just fine with Gnat 3.07 (OSF, Linux) but I think I may be just 'lucky'. The purpose of the subtype This_Arr is to make sure that the parameter passed to the C code doesn't have any 'extraneous' constraint info at the front (in fact, Gnat seems to pass the address of the first element even to the Ada procedure anyway). Gnat warns that Result is never assigned a value (that is, the Result out parameter to the Ada procedure), so I'm concerned that another compiler might handle things differently. The Rationale doesn't cover the case of array parameters (Interfaces.C.Pointers doesn't seem relevant here), but LRM B.3(70) seems to indicate that I'm OK. Any thoughts? -- with Interfaces.C; package Inter is type Arr is array (Integer range <>) of Interfaces.C.Int; procedure Accessor (Result : out Arr); end Inter; package body Inter is procedure Accessor (Result : out Arr) is subtype This_Arr is Arr (Result'Range); function Accessor (N : Interfaces.C.Int; Result : This_Arr) return Interfaces.C.Int; pragma Import (C, Accessor, "accessor"); Count : Interfaces.C.Int; begin Count := Accessor (N => Result'Length, Result => Result); end Accessor; end Inter;