From: Ted Dennison <dennison@telepath.com>
Subject: Re: raised CONSTRAINT_ERROR
Date: 2000/07/24
Date: 2000-07-24T00:00:00+00:00 [thread overview]
Message-ID: <8lhldq$ock$1@nnrp1.deja.com> (raw)
In-Reply-To: 86lmytp5i0.fsf@book.mteege.de
In article <86lmytp5i0.fsf@book.mteege.de>,
Matthias Teege <matthias@mteege.de> wrote:
> 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.
...
> StrArg : String (1 .. 10);
...
> StrArg := Ada.Command_Line.Argument(I);
That's why. Unless your command line argument happens to be *exactly* 10
characters long, you will get a constraint error here.
Check out the following entry in our FAQ:
http://www.adapower.com/lab/adafaq/24.html
Ada's string handling is very powerful. But its also very different from
most other languages. The important thing to remember is that its length
is defined by the variable's *declaration*, not by its contents.
Generally you either want to set the string's size with an
initialization:
loop
declare
StrArg : constant String := Ada.Command_Line.Argument(I);
begin
...
end;
end loop;
...or you want to declare one string big enough to hold the entire
argument, no matter how big it could reasonably get, then assign into a
correctly-sized slice of it.
Str_Arg_Length := Ada.Command_Line.Argument(I)'length;
StrArg (1..Str_Arg_Length) := Ada.Command_Line.Argument(I);
Put_Line (StrArg(1..Str_Arg_Length));
Since Ada strings don't rely on a terminator to define their length, the
'length operation does *not* iterate through the string. It might not
even generate any code at all if you have optimizations on.
Another alternative is to use a dynamic string type, like
Ada.Strings.Unbounded.Unbounded_String. Or you can use the source string
in-situ (like you did in your second example)
--
T.E.D.
http://www.telepath.com/~dennison/Ted/TED.html
Sent via Deja.com http://www.deja.com/
Before you buy.
prev parent reply other threads:[~2000-07-24 0:00 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2000-07-23 0:00 raised CONSTRAINT_ERROR Matthias Teege
2000-07-23 0:00 ` Gisle S�lensminde
2000-07-24 0:00 ` Gisle S�lensminde
2000-07-24 0:00 ` Pascal Obry
2000-07-24 0:00 ` Ted Dennison [this message]
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox