comp.lang.ada
 help / color / mirror / Atom feed
From: nobody@REPLAY.COM (Anonymous)
Subject: Re: Variable-length arrays (from a C programmer)
Date: 1998/04/24
Date: 1998-04-24T00:00:00+00:00	[thread overview]
Message-ID: <199804241446.QAA10969@basement.replay.com> (raw)
In-Reply-To: 353F9B75.3019A7A4@AlliedSignal.com


On Thu, 23 Apr 1998 14:50:13 -0500, "Paul T. McAlister"
<Paul.McAlister@AlliedSignal.com> 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/




  reply	other threads:[~1998-04-24  0:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-04-23  0:00 Variable-length arrays (from a C programmer) Paul T. McAlister
1998-04-24  0:00 ` Anonymous [this message]
1998-04-24  0:00 ` Stephen Leake
1998-04-24  0:00 ` Stephen Leake
1998-04-28  0:00 ` Matthew Heaney
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox