comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Accessing Addresses in a Passed String
Date: Sat, 1 Jan 2022 22:57:40 +0200	[thread overview]
Message-ID: <j3bti5Ff1o4U1@mid.individual.net> (raw)
In-Reply-To: <ea4fff71-c53e-4416-a345-2df277f78d0fn@googlegroups.com>

On 2022-01-01 20:53, Pat Van Canada wrote:
> Hi Everyone.
> 
> Happy New Year!
> 
> I am stuck with a problem. I have an application were I must use 
> pointers despite the fact that I try to avoid them. >
> in the declarative section, I can write:
> 
>         str2 : aliased string( 1 .. 4 ) ;
> 
> and then I could potentially access the address of each character in the string.
> 
> However, I cannot use "aliased" in an argument list and worse yet,
> since it is effectively a character array, it will be passed by pointer.


While I agree with Dmitry that it would be better to find another 
solution to the root problem (whatever that is), here are some examples 
on how to do what you have tried to do, as an example program with some 
comments.


with Ada.Text_IO;

procedure AliChar
is

    type Astring is array (Positive range <>) of aliased Character;
    --
    -- If you want to set pointers to the elements, the elements must
    -- be marked "aliased"; it is not enough (nor needed) to mark the
    -- entire array (or type) as "aliased".
    --
    -- For this we have to define our own "string" type.

    A : Astring := "This is a string with aliased elements";

    type Char_Ptr is access all Character;

    C : constant Char_Ptr := A(4)'Access;

    B : Astring renames A(6 .. 11);

    BC : constant Char_Ptr := B(9)'Access;

    D : Char_Ptr;


    procedure PP (X : access Astring)
    --
    -- For a parameter, "aliased" is not enough to allow setting a
    -- non-local pointer to point at the parameter (or some part of
    -- it), but making the parameter "access" works. But then the
    -- actual parameter must be an access value; see below.
    --
    is
    begin
       D := X(X'First + 5)'Access;
    end PP;


    AA : aliased Astring := "An aliased string with aliased elements";
    --
    -- Since AA is itself aliased, we can take AA'Access.
    -- Since AA is an Astring, we can take AA(I)'Access for any valid I.

begin

    Ada.Text_IO.Put_Line (String (A));
    --
    -- The conversion to String is needed because Astring is not the
    -- same as String. However, they are similar enough for this
    -- conversion to work, in this direction at least.

    Ada.Text_IO.Put_Line (String (AA));

    Ada.Text_IO.Put_Line ("C.all = " & C.all & ", BC.all = " & BC.all);

    PP (AA'Access);

    Ada.Text_IO.Put_Line ("D.all = " & D.all);

    D.all := '?';

    Ada.Text_IO.Put_Line (String (AA));
    --
    -- The result is "An al?ased string with aliased elements".

end AliChar;


But, again agreeing with Dmitry, this is quite messy and should be the 
last-chance solution if nothing better is found. Your description (in a 
later post) of the root problem was not clear enough for me to suggest 
something better.

  parent reply	other threads:[~2022-01-01 20:57 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-01 18:53 Accessing Addresses in a Passed String Pat Van Canada
2022-01-01 19:23 ` Dmitry A. Kazakov
2022-01-01 19:40   ` Pat Van Canada
2022-01-01 20:16     ` Dmitry A. Kazakov
2022-01-01 23:03       ` Pat Van Canada
2022-01-01 20:57 ` Niklas Holsti [this message]
2022-01-01 23:37   ` Pat Van Canada
2022-01-02 13:15   ` Pat Van Canada
2022-01-02 14:41   ` Pat Van Canada
replies disabled

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