comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: Access Type
Date: Mon, 27 Dec 2010 02:50:13 +0100
Date: 2010-12-27T02:50:13+01:00	[thread overview]
Message-ID: <87hbe0m1be.fsf@ludovic-brenta.org> (raw)
In-Reply-To: 3af2d3d1-ede9-4255-a31b-c5f66c6cee2e@c2g2000yqc.googlegroups.com

kug1977 writes on comp.lang.ada:
> Pointers are an obstacle for most newbies and it takes some time to
> understand the principle in the first time. Now I've to learn it the
> Ada-way ...

The Ada way is not to use pointers at all... you can pass a string of
characters to a C function without pointers, like this:

with Interfaces.C;
procedure P is
   procedure C_Function (String : in Interfaces.C.char_array);
   pragma Import (C, C_Function, "foo");
begin
   C_Function (Interfaces.C.To_C ("Ada string"));
end P;

This will correctly append a null character to "Ada string" and pass the
address of the result to the C_Function.

Another idea: if you have a fixed number of service names, maybe you
should encapsulate them into a package of their own and only expose an
enumerated type and a getter function, like so:

with Interfaces.C;
package Services is

   type Service is (Open, ...);

   function Name (S : in Service) return Interfaces.C.char_array;

end Services;

with Interfaces.C.Strings;
package body Services is

   type String_Access is access String;

   All_Services : constant array (Service) of Interfaces.C.Strings.chars_ptr :=
     (Open => Interfaces.C.Strings.New_String ("open"),
      ...
     );

   function Name (S : in Service) return Interfaces.C.char_array is
   begin
      return Interfaces.C.Strings.Value (All_Services (S));
   end Name;

end Services;

HTH

-- 
Ludovic Brenta.



  parent reply	other threads:[~2010-12-27  1:50 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-12-24 16:36 Access Type kug1977
2010-12-24 17:30 ` Robert A Duff
2010-12-24 20:59   ` kug1977
2010-12-24 22:06     ` Simon Wright
2010-12-27  1:50     ` Ludovic Brenta [this message]
2010-12-27 12:29       ` Simon Wright
2010-12-27 17:53         ` Ludovic Brenta
2010-12-27 18:15           ` Simon Wright
2010-12-27 20:03             ` Pascal Obry
2010-12-28 20:08               ` kug1977
2010-12-28 23:52                 ` Simon Wright
2010-12-29  8:46                   ` kug1977
2010-12-29 15:18                     ` Simon Wright
2010-12-29 15:27                       ` Ludovic Brenta
replies disabled

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