comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Wright <simon@pogner.demon.co.uk>
Subject: Interfacing to C, output arrays
Date: 1996/12/02
Date: 1996-12-02T00:00:00+00:00	[thread overview]
Message-ID: <x7viv6kmytr.fsf@pogner.demon.co.uk> (raw)


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;




             reply	other threads:[~1996-12-02  0:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-12-02  0:00 Simon Wright [this message]
1996-12-02  0:00 ` Interfacing to C, output arrays Robert Dewar
1996-12-03  0:00   ` Simon Wright
1996-12-07  0:00     ` Robert Dewar
1996-12-10  0:00 ` Paul Chardon
replies disabled

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