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-7-bit X-Google-Thread: 103376,6edeb2a39724bbc0 X-Google-Attributes: gid103376,public From: nobody@REPLAY.COM (Anonymous) Subject: Re: Variable-length arrays (from a C programmer) Date: 1998/04/24 Message-ID: <199804241446.QAA10969@basement.replay.com>#1/1 X-Deja-AN: 347312318 Content-Transfer-Encoding: 7bit References: <353F9B75.3019A7A4@AlliedSignal.com> Content-Type: text/plain; charset=us-ascii Organization: Replay Associates, L.L.P. Mail-To-News-Contact: postmaster@nym.alias.net X-001: Replay may or may not approve of the content of this posting X-002: Report misuse of this automated service to X-URL: http://www.replay.com/remailer/ Newsgroups: comp.lang.ada Date: 1998-04-24T00:00:00+00:00 List-Id: On Thu, 23 Apr 1998 14:50:13 -0500, "Paul T. McAlister" asked about variable-length arrays of variable-length arrays. This is a common problem. The solution in Ada 83 was to use discriminated records with a default discriminant value, where the discriminant determined the length of the array, thereby making the components of the array all the same type: type Message_Info is record Text : Some_Variable_Length_String_Type; Line : Positive; -- Also known as Y position Column : Positive; end record; Max_Messages_Per_Page : constant := 25; subtype Message_Num is Natural range 0 .. Max_Messages_Per_Page; type Message_List is array (Message_Num range <>) of Message_Info; type Page_Info (Num_Messages : Message_Num := 0) is record Message : Message_List (1 .. Num_Messages); end record; type Page_List is array (Positive range <>) of Page_Info; Some people didn't like this, so Ada includes access-to-objects as a way to have ragged arrays: type Message_Info is record Text : Ada.Strings.Unbounded.Unbounded_String; Line : Positive; -- Also known as Y position Column : Positive; end record; type Message_Ptr is access all Message_Info; Message_1 : aliased Message_Info := ...; Message_2 : aliased Message_Info := ...; type Page_List is array (Positive range <>) of Message_Ptr; -- Then you declare your page array: Page_1 : aliased Page_List := (Message_1'access, Message_2'access); This can be carried on for as many levels as you wish. You define an access type at each level, and an array type with the access type as the component. As long as you declare things aliased, you can get access values using 'access. Jeff Carter PGP:1024/440FBE21 My real e-mail address: ( carter @ innocon . com ) "You brightly-colored, mealy-templed, cranberry-smelling, electric donkey-bottom biters." Monty Python & the Holy Grail Posted with Spam Hater - see http://www.compulink.co.uk/~net-services/spam/