I have a program in Ada95, in which I have to create an array of strings. This array can contain strings of variable length.
Example: I have declared the array in which all the indexes can store strings of size 50. When I assign a smaller string to the above array, I get "Constraint Error".
Code:
procedure anyname is
input_array : array(1..5) of String(1..50);
begin
input_array(1):="12345";
end anyname;
I have tried to create the array of Unbounded_Strings. But that doesn't work either. Can anyone tell me how to store this "12345" in the above string array?
You can use
Ada.Strings.Unbounded
, illustrated here, or you can use a static ragged array, illustrated here. The latter approach uses an array of aliased components, each of which may have a different length.If you use
Unbounded_String
, you cannot assign a string literal to it directly. String literals can have typeString
,Wide_String
, orWide_Wide_String
, but nothing else; and assignment in Ada usually requires that the destination and source be the same type. To convert aString
to anUnbounded_String
, you need to call theTo_Unbounded_String
function:You can shorten the name by using a
use
clause; some other programmers might define their own renaming function, possibly even using the unary"+"
operator:Not everyone likes this style.