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> Lines: 44 Nf-ID: #R:hpclapd.HP.COM:920024:inmet:20600048:000:1576 Nf-From: inmet.inmet.com!stt Jun 7 13:41:00 1990 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;