comp.lang.ada
 help / color / mirror / Atom feed
* Returning a string from C
@ 2015-07-23  2:20 NiGHTS
  2015-07-23  4:48 ` Jeffrey R. Carter
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: NiGHTS @ 2015-07-23  2:20 UTC (permalink / raw)


I am having some difficulty with returning a string from a C function.

Say I have the following C function:

    void Return_Hello (char *inout_Test_String ) {
        strcpy( inout_Test_String, "Hello World" );
    }

This function will use the string memory generated on the Ada side and return a simple string "Hello World". Here is how I would interface with it:

    procedure Return_Hello ( Test_String : in out chars_ptr );
    pragma Import (C, Return_Hello, "Return_Hello");

And this is how I would use it.

    use Interfaces.C;
    use Interfaces.C.Strings;

    declare

        Test_String      : String (1 .. 100);
        Test_String_Char : chars_ptr := New_String (Test_String);

    begin
    
        Return_Hello ( Test_String_Char );
        Test_String := Value ( Test_String_Char ) ;
        Ada.Text_IO.Put_Line ( Test_String );
        Free (Test_String_Char);
  
    end;

When I run the program I get a runtime error "length check failed" on the line "Test_String := Value ( Test_String_Char );".

What am I doing wrong here?

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

end of thread, other threads:[~2015-07-23  7:17 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-23  2:20 Returning a string from C NiGHTS
2015-07-23  4:48 ` Jeffrey R. Carter
2015-07-23  4:54 ` Per Sandberg
2015-07-23  4:58 ` Laurent
2015-07-23  6:41 ` Pascal Obry
2015-07-23  7:17 ` Dmitry A. Kazakov

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