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 X-Google-Thread: 103376,f391a16b66b79f94 X-Google-Attributes: gid103376,public From: gisle@apal.ii.uib.no (Gisle S�lensminde) Subject: Re: raised CONSTRAINT_ERROR Date: 2000/07/23 Message-ID: #1/1 X-Deja-AN: 649745987 Content-Transfer-Encoding: 8bit References: <86lmytp5i0.fsf@book.mteege.de> Organization: University of Bergen, Norway Content-Type: text/plain; charset=iso-8859-1 Mime-Version: 1.0 Newsgroups: comp.lang.ada Date: 2000-07-23T00:00:00+00:00 List-Id: In article <86lmytp5i0.fsf@book.mteege.de>, Matthias Teege wrote: > >Moin, > >I made my first steps in Ada using GNATS under FreeBSD and >now I have a problem that I didnt understand. The followig >code gives me a raised CONSTRAINT_ERROR (in Line: >StrArg := Ada.Command_Line.Argument(I)) and I didnt know >why. In the first version a string of length exactly 10 is used. In the other version a generic string (allocated on stack) is used. In Ada it's not allowed to assign strings of different length, and the If you write on command line: % cldemo 0123456789 9876543210 abcdefghij The program will not give constraint error because all strings is of the correct size. The string type have fixed size. If you want to handle dynamic sized strings use unbounded strings, which is there to handle such cases. Another way is to use a block: declare Arg : String := Ada.Command_Line.Argument_Count; begin put_line(Arg); end; This way a local variable arg (with desired size) is introduced. > >with Ada.Command_Line; use Ada.Command_Line; >with GNAT.IO; use GNAT.IO; > >procedure Cldemo is > >Argcount: Natural; >StrArg : String (1 .. 10); >I: Positive := 1; > >begin > Argcount := Ada.Command_Line.Argument_Count; > >Schleife: > while I <= Argcount loop > StrArg := Ada.Command_Line.Argument(I); > Put_Line(StrArg); > I := I + 1; > end loop Schleife; > >end Cldemo; > >If I change the Code to > >with Ada.Command_Line; use Ada.Command_Line; >with GNAT.IO; use GNAT.IO; > >procedure Cldemo is > >Argcount: Natural; >I: Positive := 1; > >begin > Argcount := Ada.Command_Line.Argument_Count; > >Schleife: > while I <= Argcount loop > Put_Line(Ada.Command_Line.Argument(I)); > I := I + 1; > end loop Schleife; > >end Cldemo; > >all works perfect. What is the main difference between >both versions? > >Thanks for any hints >Matthias > -- -- Gisle S�lensminde ( gisle@ii.uib.no ) With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. (RFC 1925)