Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Strings from Ada function to C function

Tags:

c

string

ada

Ada's strings are NOT null terminated like C's. I have a requirement in a C-Ada binding application where I need to pass a string that is allocated in Ada code to a C function. How will C recognize Ada strings in here as strings in C are usually simple char array terminated by a null char while this is not the case in Ada?

Any examples would be appreciated!

like image 843
Akay Avatar asked Sep 13 '25 19:09

Akay


1 Answers

Expanding on @manuBriot's answer to your previous question on this topic, note that the convenient wrapper procedure Foo calls New_String on the incoming Ada String to obtain a chars_ptr.

procedure Foo (C : String) is
   S : Interfaces.C.Strings.chars_ptr := New_String (C);
begin
   …
end Foo;

Note also that New_String "is equivalent to New_Char_Array(To_C(Str))." When passed an Ada String, To_C sets Append_Nul to True by default. As a result, Ada Foo calls C foo with a properly null terminated C string.

like image 73
trashgod Avatar answered Sep 16 '25 09:09

trashgod