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=0.4 required=5.0 tests=BAYES_00,FORGED_MUA_MOZILLA autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,ea88e33ca617708 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.68.15.105 with SMTP id w9mr6509620pbc.7.1321646171577; Fri, 18 Nov 2011 11:56:11 -0800 (PST) Path: h5ni6701pba.0!nntp.google.com!news1.google.com!goblin1!goblin.stu.neva.ru!news.tornevall.net!.POSTED!not-for-mail From: Jeffrey Carter Newsgroups: comp.lang.ada Subject: Re: Calling Ada Procedure from C - how to handle a char ptr/array Date: Fri, 18 Nov 2011 12:56:08 -0700 Organization: TornevallNET - http://news.tornevall.net Message-ID: References: <8e5541a3-cdd5-40af-a6fd-f7539ee61326@l19g2000yqc.googlegroups.com> NNTP-Posting-Host: e4be523430866fe06bb4be21a0c02dc1 Mime-Version: 1.0 X-Trace: 8d9044e2eb30a03017477c8aadcdbf1c X-Complaints-To: abuse@tornevall.net User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.23) Gecko/20110922 Thunderbird/3.1.15 X-Complaints-Language: Spoken language is english or swedish - NOT ITALIAN, FRENCH, GERMAN OR ANY OTHER LANGUAGE! In-Reply-To: <8e5541a3-cdd5-40af-a6fd-f7539ee61326@l19g2000yqc.googlegroups.com> X-UserIDNumber: 1738 X-Validate-Post: http://news.tornevall.net/validate.php?trace=8d9044e2eb30a03017477c8aadcdbf1c X-Complaints-Italiano: Non abbiamo padronanza della lingua italiana - se mandate una email scrivete solo in Inglese, grazie X-Posting-User: 0243687135df8c4b260dd4a9a93c79bd Xref: news1.google.com comp.lang.ada:18971 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Date: 2011-11-18T12:56:08-07:00 List-Id: On 11/18/2011 12:40 PM, awdorrin wrote: > What would be the recommended way to pass a C char pointer or char > array into an Ada function or procedure that will be called from a C > program? > > After reading through the Interfaces.C.* packages, I'm assuming I > could do something like this: > > In my Ada procedure: > > procedure Get_Text( s : out Interfaces.C.char_array) is > Label : String(1..8) > begin > Label(1..8) == "12345678"; Someone's been writing C. > S := Interfaces.C.To_C(Label); > end; > > however when I compile the code I get the following warning: > warning: type of argument "Get_Text.s" is unconstrained array > warning: foreign caller must pass bounds explicitly The problem here is that Ada doesn't know the bounds of S. C has to also pass that information. > Then when I run, I get an error from the following line of code: S := > Interfaces.C.To_C(Label); > "raised CONSTRAINT_ERROR : length check failed. > > What length check? An assignment to S must have a right-hand side that's the same length as S. I suspect you'll have to assign to a slice: S (S'First .. S'First + Label'Length) := To_C (Label); > Is there a better way to be doing this? A function that returns a Char_Array might be better than an out parameter. -- Jeff Carter "Go and boil your bottoms." Monty Python & the Holy Grail 01