Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to work with arrays of strings in the C framework
#4
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.
Reply


Messages In This Thread
RE: How to work with arrays of strings in the C framework - by winteriscoming - 03-01-2023, 10:37 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)