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=0.6 required=5.0 tests=BAYES_00,LOTS_OF_MONEY, TO_NO_BRKTS_FROM_MSSP autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,e07818d50a32cdd7 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-06-04 08:43:50 PST Path: archiver1.google.com!newsfeed.google.com!sn-xit-02!sn-xit-04!supernews.com!feed.textport.net!newsranger.com!www.newsranger.com!not-for-mail Newsgroups: comp.lang.ada From: Ted Dennison References: Subject: Re: Constraint error? Message-ID: <60OS6.3835$v4.183613@www.newsranger.com> X-Abuse-Info: When contacting newsranger.com regarding abuse please X-Abuse-Info: forward the entire news article including headers or X-Abuse-Info: else we will not be able to process your request X-Complaints-To: abuse@newsranger.com NNTP-Posting-Date: Mon, 04 Jun 2001 11:42:58 EDT Organization: http://www.newsranger.com Date: Mon, 04 Jun 2001 15:42:58 GMT Xref: archiver1.google.com comp.lang.ada:8069 Date: 2001-06-04T15:42:58+00:00 List-Id: In article , 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