Re: Lenght check failed
[ Nouvelle discussion
| Répondre au groupe
|
fr.comp.lang.ada ]
Sujet: Re: Lenght check failed
De: ludo...@ludovic-brenta.org (Ludovic Brenta)
Groupes: fr.comp.lang.ada
Organisation: Tele2
Date: 09. Feb 2008, 15:09:40
References: 1 2 3 4 5
|
Tu peux déclarer une String sans spécifier sa taille à condition de
l'initialiser immédiatement avec une valeur... par exemple, le
résultat d'Ada.Strings.Unbounded.To_String.
with Ada.Strings.Unbounded;
procedure P is
Keyboard_Input : constant Ada.Strings.Unbounded.Unbounded_String :
Ada.Strings.Unbounded.Get_Line; -- longueur inconnue
S : constant String := Ada.Strings.Unbounded.To_String (Keyboard_Input);
begin
...
end P;
Maintenant si tu veux un tableau de chaînes de longueurs variables
("ragged array") il te faut un tableau de valeurs accès vers des
String. Par exemple:
with Ada.Strings.Unbounded;
with Ada.Unchecked_Deallocation;
procedure Q is
type String_Access is access String;
type String_Array is array (Positive range <>) of String_Access;
procedure Free is
new Ada.Unchecked_Deallocation (Object => String, Name => String_Access);
Keyboard_Input : Ada.Strings.Unbounded.Unbounded_String;
Inputs : String_Array (1 .. 10);
begin
Read_From_Keyboard: for J in Inputs'Range loop
Keyboard_Input := Ada.Strings.Unbounded.Get_Line; -- unknown length
Input (J) : new String'(Ada.Strings.Unbounded.To_String
(Keyboard_Input));
-- Input (J) is an access to a new string allocated on the heap with the
-- proper size
end loop Read_From_Keyboard;
-- process Inputs here
Deallocate_Each_String: for J in Inputs'Range loop
Free (Inputs (J));
end loop Deallocate_Each_String;
end Q;
Voir aussi:
http://en.wikibooks.org/wiki/Ada_Programming/Strings
http://fr.wikibooks.org/wiki/Programmation_Ada/FAQ/ (questions 2.17 et 2.6.)
--
Ludovic Brenta.

|
 cette fonctionnalité est reservée aux membres ayant une session active !
|