comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic.brenta@tiscali.be>
Subject: Re: ++ of C in ada
Date: Sun, 24 Jul 2005 20:22:20 +0200
Date: 2005-07-24T20:22:20+02:00	[thread overview]
Message-ID: <87d5p8rrmr.fsf@tiscali.be> (raw)
In-Reply-To: 1122228917.377279.262690@g47g2000cwa.googlegroups.com

"nicolas.b" <nicolas.blanpain@fr.thalesgroup.com> writes:
> How can i implement the operator ++ in ada :
>
> type T_Ptr is access all T_Item;
> How can i implement : procedure Increment (Ptr : in out T_Ptr);
> thanks

Don't do this.

In C, arrays and pointers are interchangeable, and pointer++ is used
to traverse an array.

In Ada, arrays and pointers are different.  You cannot increment a
pointer, but you can increment an array index:

type T_Item_Array is array (Positive range <>) of T_Item;

procedure Proc (A : in out T_Item_Array) is
begin
   for J in A'Range loop
      A (J) := ...
   end loop;
end Proc;

The above demonstrates how Ada handles arrays of varying sizes,
without the need for pointers (the compiler uses pointers behind the
scenes, and also ensures you don't go past the end of the array,
forget to deallocate your array, pass the wrong array size to Proc, or
other such mistakes).

If you are not doing arrays, the answer to your question is probably
still "don't do this".  If you explain what you are trying to do, we
will explain the proper Ada way (or ways) of doing it.  In general,
pointer arithmetic is useful only when interfacing directly with your
hardware.

HTH

-- 
Ludovic Brenta.



  reply	other threads:[~2005-07-24 18:22 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-07-24 18:15 ++ of C in ada nicolas.b
2005-07-24 18:22 ` Ludovic Brenta [this message]
2005-07-25  5:55 ` Jeffrey Carter
2005-07-25 13:18 ` Marc A. Criley
2005-07-25 17:00 ` Martin Krischik
2005-07-25 20:15 ` Robert A Duff
2005-08-02  5:32 ` Craig Carey
replies disabled

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