From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-2.9 required=3.0 tests=BAYES_00,NICE_REPLY_A autolearn=ham autolearn_force=no version=3.4.6 Path: eternal-september.org!reader02.eternal-september.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: Accessing Addresses in a Passed String Date: Sat, 1 Jan 2022 22:57:40 +0200 Organization: Tidorum Ltd Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net 39VGPPKEoSDiectWiQ8+NgI8foDxj1aeewuZTxciN8jbyBoLFS Cancel-Lock: sha1:0eyeldzUPWzb/fC1L6DmGMgZFdo= User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 In-Reply-To: Content-Language: en-US Xref: reader02.eternal-september.org comp.lang.ada:63323 List-Id: 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.