comp.lang.ada
 help / color / mirror / Atom feed
From: Ted Dennison<dennison@telepath.com>
Subject: Re: Constraint error?
Date: Mon, 04 Jun 2001 15:42:58 GMT
Date: 2001-06-04T15:42:58+00:00	[thread overview]
Message-ID: <60OS6.3835$v4.183613@www.newsranger.com> (raw)
In-Reply-To: fIzS6.25747$DG1.4262027@news1.rdc1.mi.home.com

In article <fIzS6.25747$DG1.4262027@news1.rdc1.mi.home.com>, McDoobie says...
>I'm really starting to like Ada, but compared to using C it still feels like 
>I'm wrestling a gorilla. I'll get it yet.

What mostly seems to be tripping you up here is string handling. You shouldn't
feel bad about that, because that's the thing that frustrates most beginners.
Ada's string handling is actually much *more* powerful and convienent than C's
in most cases, but you have to go about things quite differently. 

The main thing to realize is that an Ada string is *not* just an array of
characters; its also a range within that array (a starting and ending index). If
you don't specify that range explicitly, then most routines assume its the first
character of the array to the last. The character ASCII.NUL has *no* special
significance in Ada.

Now this philosophy forces 3 main methods for dealing with strings on you. The
first is that you declare your string arrays large enough to hold whatever you
might ever want to put in them, and keep track of the valid characters in
separate variables. Usually the start of the string can be assumed to be at the
first character ('first), so you only really have to keep track of the logical
end. This is the method that has to be used with the Text_IO.Get_* routines, as
they use this technique for their interface. Likewise, if you write your own
procedures that use a string as an "out" or "in out" parameter, you will also
need to pass out a "length" parameter.

The second way to deal with strings is to delay their declaration until you can
assign the value directly into them. That way you can leave the bounds off of
the declaration and let it size itself perfectly to the contents. This is the
easiest method to deal with, if you can use it. Most of the attributes
('whatever) that deal with strings can be used with this method. If you want to
use this method in your own subprograms, you should endevor to always return
strings as function return values. Of course this method only works if you don't
plan on changing the contents. For example (my apologies if my newsreader hoses
the formatting):

declare
Width_Answer : constant String := Answer (1..Answer_Length);
begin
-- work with Width_Answer in here.
..
end;

Now if you *do* need to change the string's contents, or have to declare it
before you can assign into it, then you can use the third way: the
Ada.Strings.Unbounded (or Ada.Strings.Bounded) package. Few predefined Ada
routines use these packages, but you may have a good use for them in your own
code.

Make sure you are acquainted with all the language defined attributes that deal
with strings and arrays. They are central to how you do strings in Ada.

As a final point, remember that strings are really just arrays, so the first two
techniques work just as well for arrays as they do for strings. :-)

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com



  reply	other threads:[~2001-06-04 15:42 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-06-03 22:30 Constraint error? McDoobie
2001-06-03 23:13 ` David C. Hoos, Sr.
2001-06-03 23:26   ` McDoobie
2001-06-04 15:42     ` Ted Dennison [this message]
2001-06-04 16:12       ` Marin David Condic
2001-06-04 20:52         ` Robert A Duff
2001-06-04 20:50       ` Robert A Duff
2001-06-04 21:13         ` Ted Dennison
2001-06-04 21:44           ` Ehud Lamm
2001-06-05 12:21       ` Marc A. Criley
2001-06-05 15:22         ` Ted Dennison
replies disabled

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