Freeplaytech Forum

Full Version: How to work with arrays of strings in the C framework
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am running into an unexpected issue when trying to store an array of text to call up in a printing function.

The printing function is:

Code:
void PrintStringLines(u8 Plane, u8 Palette, u8 XPos, u8 YPos, const char * theString)
{
   u16 * Screen;
   u8 originalXPos=XPos;

   switch (Plane)
   {
      case SCR_1_PLANE:
         Screen = SCROLL_PLANE_1;
         break;

      case SCR_2_PLANE:
         Screen = SCROLL_PLANE_2;
         break;

      default:
         return;
   }

   while (*theString)
   {
      u16 Offset = ((u16)YPos * 32) + XPos;
      u16 Value = *theString;
     
      if(Value==47){
          YPos++;
          XPos=originalXPos-1;
     
      }else{
          Value = *theString + ((u16)Palette << 9);
          Screen[Offset] = Value;
      }

      theString++;
      XPos++;
   }
}


Test 1 is to create a 2-dimensional array like this:
Code:
const char dialogueTest[][150] = {
  "Line 1 of text",
  "Line 2 of text",
  "Line 3 of text",
};

The function can be called as follows and works just fine:
PrintStringLines(SCR_2_PLANE, 0, 1, 9, dialogueTest[0]);

The only issue is that there is a lot of wasted space having to take up 150 bytes per line.

My issue is that I cannot seem to find a way to work with the data if it is defined as:
Code:
const char * dialogueTest[] = {
  "Line 1 of text",
  "Line 2 of text",
  "Line 3 of text",
};

Any tips for printing from this?  It doesn't seem to matter if I cast it.
The issue seems to occur when const char * dialogueTest[] is defined as a global variable.  In that case, it doesn't retrieve the string in the position specified.  If dialogueTest is defined inside of the function that calls it, it works... why would that be?

Edit: It works as a global variable when defined as:
const char* const dialogueTest[3] = {
  "Line 1 of text",
  "Line 2 of text",
  "Line 3 of text",
};
good to know, I checked my code and found that I was putting the string arrays inside my functions, now I remember why Smile
I fell into a similar trap with trying to define a global array of pointers to a struct.  The idea was not to waste space defining a max size for number of items in a multidimensional array.

I have this simple struct:

Code:
typedef struct {
u8 levelNum;
u8 exitNum;
} EXIT;


The individual components are defined separately as something like this:
Code:
//LEVEL0 EXITS

const EXIT LEVEL0_EXITS[]={
{LEVEL1_LEVELNUM,0},
{LEVELA0_LEVELNUM,1},
{LEVELA0_LEVELNUM,1}
};

//LEVEL1 EXITS

const EXIT LEVEL1_EXITS[]={
{LEVEL0_LEVELNUM,2},
{LEVEL5_LEVELNUM,0},
{LEVEL5_LEVELNUM,0},
{LEVEL2_LEVELNUM,2}
};



In order to get it to work and pull back the values I expected, I had to define the global array as: 
const EXIT* const exitMap[]
Code:
const EXIT* const exitMap[]={
LEVEL0_EXITS,
LEVEL1_EXITS,
....
};


So this is something to keep in mind going forward.  I spent some frustrating time trying to figure out why I wasn't getting the values from the array that I expected, when it had been working if I defined it as a multidimensional array with a set max number of positions.