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.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!cs.utexas.edu!samsung!zaphod.mps.ohio-state.edu!rpi!bu.edu!inmet!stt From: stt@inmet.inmet.com Newsgroups: comp.lang.ada Subject: Re: C Strings in Ada? Message-ID: <20600048@inmet> Date: 7 Jun 90 17:41:00 GMT References: <920024@hpclapd.HP.COM> Nf-ID: #R:hpclapd.HP.COM:920024:inmet:20600048:000:1576 Nf-From: inmet.inmet.com!stt Jun 7 13:41:00 1990 List-Id: Re: C_Strings in Ada The simplest way to turn an Ada string into a string acceptable to C is: STR & ASCII.NUL This works quite well in several compilers, and only requires that the called C code ignore the additional array descriptor often passed along with an unconstrained array. On the other hand, if you want a type which you can manipulate in Ada which represents a null-terminated string of unknown length, it is best to define an access type to a *constrained* string subtype. You can then pretty safely use unchecked conversion to convert the address of the C string into a value of this access type. You may then refer to the characters of the C string so long as the C string does not exceed the length of the constrained string subtype. For instance: type C_String_Ptr is access String(1..Integer'LAST); function To_C_String_Ptr is new Unchecked_Conversion( System.Address, C_String_Ptr ); Addr : System.Address; Ptr : C_String_Ptr; begin Addr :=
Ptr := To_C_String_Ptr(Addr); for I in Ptr'RANGE loop exit when Ptr(I) = ASCII.NUL; end loop; If you use unchecked conversion between an address and an access to an unconstrained array type, on many compilers all hell breaks loose, because the access value is assumed to be pointing at a descriptor, but you have set it to point directly at data. BTW, None of this works on a RATIONAL, at least not until they get their C++ compiler working :-*. S. Tucker Taft Intermetrics, Inc. Cambridge, MA 02138