comp.lang.ada
 help / color / mirror / Atom feed
* Interfacing to C
@ 2009-12-24  9:46 RasikaSrinivasan@gmail.com
  2009-12-24 10:01 ` Hibou57 (Yannick Duchêne)
                   ` (2 more replies)
  0 siblings, 3 replies; 17+ messages in thread
From: RasikaSrinivasan@gmail.com @ 2009-12-24  9:46 UTC (permalink / raw)


I am building an interface to a C library. This library uses structs
like the following:

typedef struct {
    x : int ;
    y : int }  MyStruct ....

While passing variables of this type to functions, the convention in C
is to pass by value (best of my knowledge).

however when I import a function like:

type MyStruct is record ..... end record ;
pragma Convention(C,MyStruct);

procedure process(var : MyStruct);
pragma Import(C,process);

It appears that Ada passes the var by reference. (Well - I am not able
to tell for sure - just that modifying the binding as follows :

procedure process(varx : int; vary : int);
pragma Import(C,process) ;

works and the previous binding did not.

So the question is - how c(an I force Ada (gnat) to pass a record by
value?

thanks for pointers, srini




^ permalink raw reply	[flat|nested] 17+ messages in thread
* Interfacing to C
@ 2005-02-08 18:07 Garry
  2005-02-08 18:43 ` Georg Bauhaus
                   ` (3 more replies)
  0 siblings, 4 replies; 17+ messages in thread
From: Garry @ 2005-02-08 18:07 UTC (permalink / raw)


I'm just starting in ADA.  Can anyone explain why I have to define a
dummy procedure in my hsdbg package to get my code to work?

Error without dummy procedure:
hsdbg.ads:4:09: package "HSDBG" does not allow a body
hsdbg.ads:4:09: remove incorrect body in file "hsdbg.adb"
gnatmake: "hsdbg.ads" compilation error
error: "hsdbg.adb" must be recompiled ("hsdbg.ads" has been modified)
gnatlink: Failed to open binder output

Code follows:
/* Start of marktime.c */
#include <stdio.h>

void marktime(char  str[255])
{
   printf("marktime: str='%s'\n",str);
}
/* End of marktime.c */



-- Start of hsdbg.ads
--
with Interfaces.C;
with Interfaces.C.Strings;

package HSDBG is
--  Dummy procedure?
      type DUMB is digits 5 range 0.0 .. 1.0E10;
      procedure DUMMY (B : DUMB);
--  End Dummy procedure
      procedure marktime(Variable : Interfaces.C.Strings.chars_ptr);
      pragma Import(C, marktime, "marktime");
end HSDBG;
-- End of hsdbg.ads



-- Start of hsdbg.adb
--
with Ada.Text_IO; use Ada.Text_IO;
with Interfaces.C.Strings; use Interfaces.C.Strings;
--
--  Define package body
--
package body HSDBG is
--
--
--  dummy procedure?
  procedure DUMMY(B : DUMB) is
  begin
     put("In Dummy");
  end DUMMY;
--  end dummy procedure

   procedure marktime(Variable : String) is

      procedure marktime(Variable : chars_ptr);
      pragma Import(C, marktime, "marktime");

      Variable_In_C_Format : chars_ptr := New_String(Variable);


   begin
      marktime(Variable_In_C_Format);
      Free(Variable_In_C_Format);
   end marktime;


 end HSDBG;
-- End of hsdbg.adb




-- Start of tst.adb
with Ada.Text_IO; use Ada.Text_IO;
with Interfaces.C.Strings; use Interfaces.C.Strings;
with hsdbg; use hsdbg;

procedure tst is

  C_Format : chars_ptr := New_String("This is a string in ADA that is
written by the marktime C procedure");

begin
   Put_Line ("test started");
   marktime(C_Format);

end tst;
-- End of tst.adb




^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2010-01-01 18:02 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-12-24  9:46 Interfacing to C RasikaSrinivasan@gmail.com
2009-12-24 10:01 ` Hibou57 (Yannick Duchêne)
2009-12-25  2:07   ` Keith Thompson
2009-12-31 23:18     ` Robert A Duff
2010-01-01 18:02       ` Keith Thompson
2009-12-24 12:34 ` Niklas Holsti
2009-12-24 14:26   ` RasikaSrinivasan@gmail.com
2009-12-28  7:20   ` Hibou57 (Yannick Duchêne)
2009-12-24 12:35 ` Vadim Godunko
  -- strict thread matches above, loose matches on Subject: below --
2005-02-08 18:07 Garry
2005-02-08 18:43 ` Georg Bauhaus
2005-02-08 19:01 ` Ludovic Brenta
2005-02-08 19:27 ` Martin Krischik
2005-02-08 20:50 ` Björn Lundin
2005-02-09  7:27   ` Anders Wirzenius
2005-02-09 18:53     ` Garry
2005-02-09 21:35       ` Björn Lundin

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