user3491848
user3491848

Reputation: 1

Declaring array of fixed length strings using parameters for length and size

I would like to be able to declare a constant array of fixed length string for a GTK ComboBox. Is it possible to do with two parameters in ADA?

This code allows the size of the array to be set by declaring the strings. Is it possible to replace the constant in the spec with a parameter somehow?

A simple array does not allow a discriminant. A discriminant record does not permit an anonymous array.

Edit:

Can I somehow declare a type equivalent to the form:

type ArrayOfStrings( sringlength : positive; arraysize : positive ) array( 1..arraysize ) of string( 1..stringlength );

This form is not possible because an array can not have a discriminant.

OR

subtype SelectorString is string;

type SelectorStringArray( arraysize : positive; stringlength : positive ) is record stringarray : array( 1..arraysize ) of SelectorString( 1..stringlength ); end record;

This form is not possible because a record can not have anonymous arrays.

Answered:

The following works,

package Selectors.Strings is

subtype ArrayIndex is positive range 1..64; -- maximum entries in ComboBox

subtype TextIndex is positive range 1..32; -- maximum entry length

type SelectorStringArray is array( arrayindex range <>, textindex range <> ) of character;

function StringSelector( stringarray : SelectorStringArray ) return ArrayIndex;

sel : SelectorStringArray := ( "aa", "bb", "cc", "dd" );

end Selectors.Strings;

The strings in sel must all be the same length.

Thanks,

old engineer

package Selectors.Strings is

    SelectorStringLength : constant := 2;

    subtype SelectorString is string( 1..SelectorStringLength );

    type    SelectorStringArray is array( positive range <> ) of SelectorString;

    subtype SelectorStringIndex is positive;

    function    StringSelector( stringarray : SelectorStringArray ) return SelectorStringIndex;

    selectarray : SelectorStringArray := ( "aa", "bb", "cc", "dd" );

end Selectors.Strings;

Upvotes: 0

Views: 709

Answers (1)

ajb
ajb

Reputation: 31699

I'm not clear on what you're looking for, but you don't have any anonymous arrays in the above code, and you can have a discriminant record type whose component is a named array type. So I'm not sure what your statement "A discriminant record does not permit an anonymous array" is referring to.

Suppose selectarray can have at most 100 strings:

subtype Select_Array_Size is Integer range 0 .. 100;  

type Selector_Strings (Size : Select_Array_Size := 0) is record
    Data : SelectorStringArray (1 .. Size);
end record;

procedure Set_Select_Array (Data : SelectorStringArray);

selectarray : Selector_Strings;  -- will be initialized with Size=>0, 
                                 -- Data=>an array of length 0

and in the body

procedure Set_Select_Array (Data : SelectorStringArray) is
begin
    selectarray := (Size => Data'Length, Data => Data);
end Set_Select_Array;

and then use selectarray.Data instead of selectarray to access the data.

You need a maximum array length because some compilers will allocate as much space as the maximum possible size for the Data component of Selector_Strings.

The other option is to make selectarray an access to the array type.

Upvotes: 0

Related Questions