From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Jeffrey Carter Newsgroups: comp.lang.ada Subject: Re: Passing a String to a C/C++ Subprogram (Special Case) Date: Tue, 15 Oct 2013 13:59:21 -0700 Organization: Also freenews.netfront.net; news.tornevall.net Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 15 Oct 2013 20:59:23 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="1f3496dec3b2a953772d1e0537164117"; logging-data="27007"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1844cenL4OrZ0qklnaNWXTEcy9tgmcpIdE=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.0 In-Reply-To: Cancel-Lock: sha1:B3nDLLJidg4XzsAbyBAzcPTT1SQ= Xref: news.eternal-september.org comp.lang.ada:17427 Date: 2013-10-15T13:59:21-07:00 List-Id: 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