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,ed3864cb23152ae6 X-Google-Attributes: gid103376,public From: mab@dst17.wdl.loral.com (Mark A Biggar) Subject: Re: C++'s pointer vs Ada's Access type Date: 1996/08/01 Message-ID: <4tqkns$o2u@wdl1.wdl.lmco.com>#1/1 X-Deja-AN: 171423305 references: <31FFD4A6.41C6@afit.af.mil> organization: Loral Western Development Labs newsgroups: comp.lang.ada Date: 1996-08-01T00:00:00+00:00 List-Id: In article stt@henning.camb.inmet.com (johndoe) writes: >Ding-yuan Sheu (dsheu@afit.af.mil) wrote: >: I think C++'s pointer type is basically similar to Ada's access >: type. Therefore, for the following C++ declaraction: >: int* ip; >: I convert it into Ada: >: type Integer_Ptr is access all Integer; >: ip : Integer_Ptr; >: However, there comes a problem. >: In C++, ip can be a pointer that points to a integer array. >: Therefore, C++ programmers can do the following initialization: >: int Array[100]; >: ip = &Array; >: for (i:=0;i++,99) { (*ip+i) = 0; } >: My question is how I can do the same thing in Ada by using ip >: to initialize Array instead to directly initialize Array. >This is somewhat odd looking C/C++ code. I would have expected: > for (ip = Array; ip < &Array[100]; ) *ip++ = 0; >In any case, the corresponding Ada would generally not use >pointers at all: > An_Array : array (1..100) of Integer; > > for I in An_Array'Range loop > An_Array[I] := 0; > end loop; Or even more likely: An_Array : array (1..100) of Integer := (1..100 => 0); -- Mark Biggar mab@wdl.lmco.com > >If you are intent on using a pointer, the following would work, though >I can't imagine a case where it would be preferable to the above. > > type Int_Array is array(1..100) of Integer; > > type Array_Ptr is access all Int_Array; > An_Array : aliased Int_Array; > IP : Array_Ptr := An_Array'Access; > > for I in IP.all'Range loop > IP.all[I] := 0; > end loop; > > (both of the "IP.all" above could be shortened to simply "IP") > >: I know my question might be stupid for you Ada experts. >: Your comments are preciou to me. Any Help will be appreciated. > >Generally pointers to arrays are not used very much in Ada. >Pointers to records are used, when you are allocating objects on the heap. >C/C++ programmers sometimes use pointers to perform manual "strength >reduction" when manipulating arrays, but in Ada one generally >just uses normal array indexing, while relying on the compiler to >use pointer arithmetic at the machine level as appropriate. > >: Steven > >-Tucker Taft stt@inmet.com http://www.inmet.com/~stt/ >Intermetrics, Inc. Cambridge, MA USA