comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: Passing a String to a C/C++ Subprogram (Special Case)
Date: Tue, 15 Oct 2013 13:59:21 -0700
Date: 2013-10-15T13:59:21-07:00	[thread overview]
Message-ID: <l3kab9$qbv$1@dont-email.me> (raw)
In-Reply-To: <e87e8a92-9822-4764-a4a6-1fbc6715ef52@googlegroups.com>

On 10/15/2013 12:54 PM, Eryndlia Mavourneen wrote:
>
> Q: Is there any reason that I cannot just declare each of them in this way, assuming the subprogram is written in C/C++:
>
>       . . .
>          A_String : String (1 .. 30);
>          pragma Convention (C, A_String);
>          . . .
>          procedure C_Prog (C_String : in String);
>          pragma Import (Entity => C_Prog, ...);
>       begin
>          C_Prog (C_String => A_String);
>       . . .
>
> and have it work properly?
>
> This method, of course, leaves it up to the client developer to insert any necessary nul characters to terminate the string, if is is smaller than the declared bound.

As Beneschan pointed out, this should work, assuming your compiler equates a 
Character to a C char, and C_Prog expects a string of exactly 30 characters (in 
which case a convention-C subtype for the parameter would be in order) or a 
NUL-terminated variable-length string and the caller remembers to put a NUL in 
the passed string.

Relying on any of that is asking for trouble, especially if this is, as you 
imply, part of a larger project. Eventually this will be modified by someone who 
will get it wrong. Much safer is to use the facilities of Interfaces.

If I were doing this, I would wrap each in an Ada subprogram that ensures the 
String will be passed correctly to C. Ultimately it will be less effort.

-- 
Jeff Carter
"Help! Help! I'm being repressed!"
Monty Python & the Holy Grail
67

  parent reply	other threads:[~2013-10-15 20:59 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-15 19:54 Passing a String to a C/C++ Subprogram (Special Case) Eryndlia Mavourneen
2013-10-15 20:11 ` Adam Beneschan
2013-10-15 21:02   ` Eryndlia Mavourneen
2013-10-16  7:17     ` Dmitry A. Kazakov
2013-10-16 15:14       ` Jeffrey Carter
2013-10-16 17:32     ` Martin
2013-10-16 17:42       ` Eryndlia Mavourneen
2013-10-16 17:54         ` Martin
2013-10-16 18:13           ` Eryndlia Mavourneen
2013-10-16 19:40             ` Simon Wright
2013-10-16 20:02               ` Eryndlia Mavourneen
2013-10-19  2:11           ` Randy Brukardt
2013-10-16 19:11       ` Adam Beneschan
2013-10-16 21:31         ` Martin
2013-10-16 21:41           ` Adam Beneschan
2013-10-16 23:15             ` Martin
2013-10-15 20:59 ` Jeffrey Carter [this message]
2013-10-15 21:13   ` Eryndlia Mavourneen
2013-10-15 22:19     ` Shark8
2013-10-16  0:00     ` Jeffrey Carter
2013-10-17  9:55     ` Georg Bauhaus
2013-10-16 20:17 ` sbelmont700
2013-10-16 20:48   ` Adam Beneschan
2013-10-17 13:05     ` Eryndlia Mavourneen
2013-10-17 13:58       ` sbelmont700
2013-10-17 16:28         ` Eryndlia Mavourneen
replies disabled

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