comp.lang.ada
 help / color / mirror / Atom feed
From: stt@inmet.inmet.com
Subject: Re: C Strings in Ada?
Date: 7 Jun 90 17:41:00 GMT	[thread overview]
Message-ID: <20600048@inmet> (raw)
In-Reply-To: 920024@hpclapd.HP.COM


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 := <address of C-string>
    Ptr := To_C_String_Ptr(Addr);
    for I in Ptr'RANGE loop
        exit when Ptr(I) = ASCII.NUL;
        <do something with Ptr(I)>
    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

  reply	other threads:[~1990-06-07 17:41 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1990-06-04 22:45 C Strings in Ada? Andy DeFaria
1990-06-07 17:41 ` stt [this message]
1990-06-08 16:00 ` Andy DeFaria
1990-06-10 20:38   ` Alex Blakemore
1990-06-11 12:57     ` Allan Doyle
1990-06-11 14:59       ` David Kassover
1990-06-11 19:48         ` Allan Doyle
1990-06-11 21:01           ` David Kassover
1990-06-11 22:30           ` Mike Murphy
1990-06-13 21:20           ` Edward Falis
1990-06-11 17:53       ` David Emery
1990-06-11 19:59         ` Allan Doyle
1990-06-15  7:53   ` Jeff Bartlett
1990-06-11 22:39 ` Andy DeFaria
1990-06-12 14:04   ` David Emery
1990-06-12 18:11   ` Mike Murphy
1990-06-13 13:43 ` stt
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox