comp.lang.ada
 help / color / mirror / Atom feed
From: stt@henning.camb.inmet.com (johndoe)
Subject: Re: C++'s pointer vs Ada's Access type
Date: 1996/08/01
Date: 1996-08-01T00:00:00+00:00	[thread overview]
Message-ID: <DvFu6E.Atz.0.-s@inmet.camb.inmet.com> (raw)
In-Reply-To: 31FFD4A6.41C6@afit.af.mil


Ding-yuan Sheu (dsheu@afit.af.mil) wrote:


: 	I am doing some C++ programs conversion into Ada95 and I have
: a problem in converting C++ pointer into Ada95's access type. I am not
: an Ada expert. I hope someone can give me a hand.

: 	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;

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




  reply	other threads:[~1996-08-01  0:00 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-07-31  0:00 C++'s pointer vs Ada's Access type Ding-yuan Sheu
1996-08-01  0:00 ` johndoe [this message]
1996-08-01  0:00   ` Mark A Biggar
1996-08-01  0:00   ` Bryce Bardin
1996-08-01  0:00   ` Fraser Wilson
1996-08-01  0:00 ` David Weller
replies disabled

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