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=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC 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: Stephen Leake Subject: Re: Variable-length arrays (from a C programmer) Date: 1998/04/24 Message-ID: <3540BF46.6D61@gsfc.nasa.gov>#1/1 X-Deja-AN: 347338113 Content-Transfer-Encoding: 7bit References: <353F9B75.3019A7A4@AlliedSignal.com> Content-Type: text/plain; charset=us-ascii Organization: NASA Goddard Space Flight Center -- Greenbelt, Maryland USA Mime-Version: 1.0 Reply-To: stephen.leake@gsfc.nasa.gov Newsgroups: comp.lang.ada Date: 1998-04-24T00:00:00+00:00 List-Id: Paul T. McAlister wrote: > > I have an application where I will be displaying a number of different > screens full of text messages. > I want these messages and screens to be data table driven. > > In "C" (see example), I can define an array of messages for each page, > then define an array of pages for the whole thing. > I don't have to worry about string length or number of items defined in > the arrays, the compiler handles all of that for me. > > I have not been able to figure out how to do the equivalent thing in > Ada. > I don't know how to define strings without setting the length to the > maximum length string I will have, which wastes a lot of space. > I think I know how to define the variable length arrays for the messages > for each screen, but I don't know how to define an array type of these > arrays of string for each page (since they are different types because > they are different lengths). > I am using Ada83. > > I know this is a lengthy question, but any help would be greatly > appreciated. Here's one way. It allocates the strings at elaboration time. Note that Ada can determine the size of an array from its initializer (like a C string, but unlike a C array). with Ada.Text_IO; use Ada.Text_IO; procedure Test is -- structure for each page type String_Ptr is access String; type Msg_Struct is record Text : String_ptr; -- pointer to message text XPos : integer; -- x position on screen YPos : integer; -- y position on screen end record; type Msg_Struct_Array is array (Integer range <> ) of Msg_Struct; type Msg_Struct_Array_Ptr is access Msg_Struct_Array; type Page_Struct is array (Integer range <> ) of Msg_Struct_Array_ptr; Pages : constant Page_Struct := (-- page 1 messages new Msg_Struct_Array' ((new String'("Greetings Earthling"), 0, 0), (new String'("Take me to your leader"), 1, 0)), -- page 2 messages new Msg_Struct_Array' ((new String'("Screen 2"), 0, 0), (new String'("This text is some lines farther down"), 5, 0), (new String'("and this is even FARTHER down"), 10, 0)), -- page 3 messages new Msg_Struct_Array' ((new String'("This is the third screen"), 0, 0), (new String'("and by now you should be getting"), 1, 10), (new String'("the point of what it is"), 2, 12), (new String'("I am trying to do!!"), 3, 14)) ); IPage : integer; IMsg : integer; NPage : integer; begin -- find number of pages nPage := Pages'length; -- for each page for iPage in Pages'range loop New_Line; -- put blank line between pages -- for each message for iMsg in Pages(IPage)'Range loop Put (Pages(IPage).all(IMsg).Text.all); -- print message New_Line; -- terminate line end loop; end loop; end Test; This allocates everything at elaboration time; a smart compiler could do it at compile or link time. To get closer to the C semantics (no explicit allocation), you have to introduce more Ada names: Page1_Message1 : constant aliased String := "Greetings Earthling"; Message1 : aliased Msg_Struct := (Page1_Message1'access, 0, 0); etc. -- Stephe