From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,1fd537a3da8a5021,start X-Google-Attributes: gid103376,public From: Michael Carman Subject: Array access via pointer? Date: 2000/08/01 Message-ID: <398721AD.CAEE3A51@home.com>#1/1 X-Deja-AN: 653330768 Content-Transfer-Encoding: 7bit Organization: None (anarchist) X-Accept-Language: en Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 Newsgroups: comp.lang.ada Date: 2000-08-01T00:00:00+00:00 List-Id: First, a warning: my knowledge of Ada is pretty rudimentary, and I tend to use the terms I'm familiar with from other languages. If words like "pointer" and "reference" betray that background, oh well. Quick overview: I have several arrays, each holding the same type of data (system addresses) but the arrays are of different sizes. I want to have a single procedure to process the data in these arrays. (For various reasons, the numbers/sizes of arrays are necessary, so no paradigm shifts allowed there!) In order to do this I pass my procedure a pointer to an array and an index into it. It looks something like this: type Addr_Table is array (Natural range <>) of System.Address; Arr1 : Addr_Table (0 .. 100); Arr2 : Addr_Table (0 .. 20); procedure Foo (Addr : in System.Address; I : in Integer) is begin -- Do something with index I of the array at Addr end Foo; A call to it might look like Foo (Arr2'Address, 7). Inside Foo, I tried to get at the array data in this manner: Data := Addr_To_Ptr(Addr).all (Idx); where type Addr_Table_Ptr is access Addr_Table; function Addr_To_Ptr is new Unchecked_Conversion (System.Address, Addr_Table_Ptr); but that gave me a constraint error, apparently because type Addr_Table is unconstrained and the instantiaions of it are not. Yes, if Addr_Table had a definate size this would be easy, but it doesn't and (for me) it's not. The only way I've been able to make it work is with this: Data := Addr_To_Ptr(Addr + Idx * Size_In_Words).all; where type Addr_Ptr is access System.Address; function Addr_To_Ptr is new Unchecked_Conversion (System.Address, Addr_Ptr); Size_In_Words := System.Word_Size / System.Storage_Unit; but that seems unnecessarily messy. Well, it looks like C, which wouldn't really bother me but I'm not writing C at the moment. It seems to me that there should be a way to do this without having to fight against the language. So, what's the canonical way of doing this in Ada? -mjc