comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Pointer to Array
Date: Tue, 16 Oct 2007 17:09:18 +0300
Date: 2007-10-16T17:09:18+03:00	[thread overview]
Message-ID: <4714c406$0$27840$39db0f71@news.song.fi> (raw)
In-Reply-To: <1192542030.745091.64900@q5g2000prf.googlegroups.com>

shaunpatterson@gmail.com wrote:
> I'm attempting to make a pointer to an array... or in a sense
> "rename" an array.
> 
> I'm combining code from two very similar systems.  Lots of code.
> 
> I have thousands of lines of code that's exactly the same, using
> (for just an example) an array of Integers.
> 
> There is a store file in each system that declares:
> 
> -- system 1
>     type INT_ARRAY is new Integer (1..SYSTEM_1_SIZE);

That declaration of INT_ARRAY is not correct -- you must have made 
a typo of some sort in writing your post. What is the real declaration?

> I want to keep that code untouched if possible.  I'm hoping there is a
> way
> in Ada to do something like
> 
>     type INT_ARRAY is array (Positive range <>) of Integer;
>     Int_Array_Var_System1 : INT_ARRAY (1..System_1_Size);
>     Int_Array_Var_System2 : INT_ARRAY (1..System_2_Size);
> 
> 
> And I'd like to so something at run time that is like:
> 
>     if usingSystem1 then
>         Int_array_Var = "pointer to Int_Array_Var_System1"
>     elsif usingSystem2 then
>         Int_array_Var = "pointer to Int_Array_Var_System2"
> 
> by changing the pointer I'm hoping to keep most of existing code the
> same.
> 
> Is there any method in ada to do this?

Better method: Make the the code into a procedure that takes the 
parameter as an array, then call the procedure either with 
Int_Array_Var_System1 or Int_Array_Var_System2 as the actual 
parameter. But this can be difficult if you have several such pairs 
of arrays, and the condition for choosing system1/2 is not the same 
for all pairs -- you can get a large number of combinations.

Second method, using an access variable (the Text_IO and 
Command_Line are there just for testing):

with Ada.Text_IO;
with Ada.Command_Line;

procedure IntArr
is

     type INT_ARRAY is array (Positive range <>) of Integer;

     System_1_Size : constant := 55;
     System_2_Size : constant := 77;

     Int_Array_Var_System1 : aliased INT_ARRAY :=
        (1..System_1_Size => 0);
     Int_Array_Var_System2 : aliased INT_ARRAY :=
        (1..System_2_Size => 0);

     type INT_ARRAY_REF is access all INT_ARRAY;

     Int_Array_Var : INT_ARRAY_REF;

begin

    if Ada.Command_Line.Argument_Count > 0 then

       Int_Array_Var := Int_Array_Var_System1'Access;

    else

       Int_Array_Var := Int_Array_Var_System2'Access;

    end if;

    Ada.Text_IO.Put_Line (
       "Length" & Natural'Image (Int_Array_Var'Length));

end IntArr;


HTH...

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .



  reply	other threads:[~2007-10-16 14:09 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-10-16 13:40 Pointer to Array shaunpatterson
2007-10-16 14:09 ` Niklas Holsti [this message]
2007-10-16 14:16 ` Robert A Duff
replies disabled

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