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.41 with SMTP id u9mr7093839pbc.3.1321655552264; Fri, 18 Nov 2011 14:32:32 -0800 (PST) Path: h5ni7154pba.0!nntp.google.com!news2.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!weretis.net!feeder4.news.weretis.net!news.teledata-fn.de!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail Date: Fri, 18 Nov 2011 23:32:24 +0100 From: Georg Bauhaus User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1 MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Calling Ada Procedure from C - how to handle a char ptr/array References: <8e5541a3-cdd5-40af-a6fd-f7539ee61326@l19g2000yqc.googlegroups.com> In-Reply-To: Message-ID: <4ec6dcf9$0$6634$9b4e6d93@newsspool2.arcor-online.net> Organization: Arcor NNTP-Posting-Date: 18 Nov 2011 23:32:25 CET NNTP-Posting-Host: 103c6414.newsspool2.arcor-online.net X-Trace: DXC=A:gNT\OMV_FC4i^e1BZ=_HA9EHlD;3YcB4Fo<]lROoRA8kFejVHPafB8Z`=_LF2X On 18.11.11 21:41, awdorrin wrote: > function Get_Label return Interfaces.C.Char_array is > Label : String(1..8); > begin > Label(1..8) := "12345678"; > > return Interfaces.C.To_C(Label); > end; While only partly following GNAT's advice to not have Get_Label return an unconstrained array (not following because this requires defining a constrained subtype somewhere and I was lazy), this works for me. It works with or without defining a constrained Result array in Get_Label. with Interfaces.C; function Get_Label return Interfaces.C.Char_Array; pragma Export (C, Get_Label, "get_label"); function Get_Label return Interfaces.C.Char_array is Label : String(1..8); Result : Interfaces.C.char_array(0..8); begin Label(1..8) := "12345678"; Result := Interfaces.C.To_C(Label); return Result; end; #include int main() { extern char* get_label(); extern void adainit(void); extern void adafinal(void); char* label; adainit(); label = get_label(); adafinal(); fputs(label, stdout); return 0; } $ gnatmake -c -gnatwa -fstack-check -gnato -f get_label.adb gcc -c -gnatwa -fstack-check -gnato get_label.adb get_label.ads:2:10: warning: foreign convention function "Get_Label" should not return unconstrained array $ cc -W -c with_label.c $ gnatbind -n get_label.ali $ gnatlink get_label.ali with_label.o -o test_get_label $ ./test_get_label 12345678$