comp.lang.ada
 help / color / mirror / Atom feed
* Variable-length arrays (from a C programmer)
@ 1998-04-23  0:00 Paul T. McAlister
  1998-04-24  0:00 ` Stephen Leake
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Paul T. McAlister @ 1998-04-23  0:00 UTC (permalink / raw)



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.

- - - - - - - - - - - -

#include <stdio.h>

// structure for each message
typedef struct
{
  char *text;   // pointer to message text
  int   xPos;   // x position on screen
  int   yPos;   // y position on screen
}
MSG_STRUCT;

// structure for each page
typedef struct
{
  MSG_STRUCT  *msg;      // pointer to array of message structures
  int         num_msgs;   // number of message on this page
}
PAGE_STRUCT;

// page 1 messages
MSG_STRUCT page1[] =
{ {"Greetings Earthling", 0, 0}
, {"Take me to your leader", 1, 0}
};

// page 2 messages
MSG_STRUCT page2[] =
{ {"Screen 2", 0, 0}
, {"This text is some lines farther down", 5, 0}
, {"and this is even FARTHER down", 10, 0}
};

// page 3 messages
MSG_STRUCT page3[] =
{ {"This is the third screen", 0, 0}
, {"and by now you should be getting", 1, 10}
, {"the point of what it is", 2, 12}
, {"I am trying to do!!", 3, 14}
};

// messages for all pages
PAGE_STRUCT pages[] =
{ {page1, (sizeof(page1) / sizeof(page1[0]))}
, {page2, (sizeof(page2) / sizeof(page2[0]))}
, {page3, (sizeof(page3) / sizeof(page3[0]))}
};

void main()
{
  int iPage;
  int iMsg;
  int nPage;

  // find number of pages
  nPage = sizeof(pages) / sizeof(pages[0]);

  // for each page
  for (iPage = 0; iPage < nPage; ++iPage)
  {
    printf("\n");   // put blank line between pages

    // for each message
    for (iMsg = 0; iMsg < pages[iPage].num_msgs; ++iMsg)
    {
      printf(pages[iPage].msg[iMsg].text);  // print message
      printf("\n");                         // terminate line
    }
  }
}

  --PTM                                   8-)
--------------------------------------
Paul McAlister
Paul.McAlister@AlliedSignal.com
913.768.2238
--------------------------------------






^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~1998-04-28  0:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-04-23  0:00 Variable-length arrays (from a C programmer) Paul T. McAlister
1998-04-24  0:00 ` Stephen Leake
1998-04-24  0:00 ` Stephen Leake
1998-04-24  0:00 ` Anonymous
1998-04-28  0:00 ` Matthew Heaney

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