comp.lang.ada
 help / color / mirror / Atom feed
* Problem when interfacing C with Ada
@ 1996-08-29  0:00 Jean-Marc Bourguet
  0 siblings, 0 replies; only message in thread
From: Jean-Marc Bourguet @ 1996-08-29  0:00 UTC (permalink / raw)



When compiling the following program which interfaces C with Ada, I
don't optain the expected result; the display of

an Ada string
a C string

But I get

an Ada string
<an empty line>

I'm using gnat 3.05 on an Sun running Solaris 2.5.

I'd appreciate any help.

Here are the files:

-- main.adb
with Util;

procedure Main is
   S : Util.String_Pointer;
begin
   S := new String'("an Ada string");
   Util.Display_String_Pointer (S);
   Util.C_Function;
end Main;

-- util.ads
with Interfaces.C.Strings;

package Util is

   package C
     renames Interfaces.C;

   type String_Pointer is access String;

   function  To_String_Pointer (S : C.Strings.Chars_Ptr) return String_pointer;
   pragma Export (C, To_String_Pointer, "to_string_pointer");

   procedure Display_String_Pointer (S : String_Pointer);
   pragma Export (C, Display_String_Pointer, "display_string_pointer");

   procedure C_Function;
   pragma Import (C, C_Function, "c_function");

end Util;

-- util.adb
with Interfaces.C.Strings;
with Ada.Text_Io;

package body Util is

   function  To_String_Pointer (S : C.Strings.Chars_Ptr) return String_pointer
   is
   begin -- To_String_Pointer
      return new String'(C.Strings.Value (S));
   end To_String_Pointer;

   procedure Display_String_Pointer (S : String_Pointer)
   is
   begin -- Display_String_Pointer
      Ada.Text_Io.Put_Line (S.all);
   end Display_String_Pointer;

end Util;

/* c_function.c */
typedef struct FAT_POINTER { char *a; void *b;} string_pointer;

string_pointer to_string_pointer (char *);
void display_string_pointer (string_pointer);

void c_function (void)
{
  string_pointer string;

  string = to_string_pointer ("a C string");

  display_string_pointer (string);

}
-- 
 Jean-Marc Bourguet                      Service d'Electronique
                                         Faculte polytechnique de Mons
 Email : bourguet@muelec.fpms.ac.be      Mons, Belgique




^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~1996-08-29  0:00 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-08-29  0:00 Problem when interfacing C with Ada Jean-Marc Bourguet

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