Link to home
Start Free TrialLog in
Avatar of CABrown
CABrown

asked on

Dynamically allocated array of structs

How do I properly access the members of a dynamically allocated array of structs?

My struct:

struct wumpusWorldCell{
char state;
bool wumpus;
bool gold;
};


My class:

class wumpusWorld{
wumpusWorldCell **cave;
...
};


My constructor that creates the array:

wumpusWorld::wumpusWorld(int m, int n){
cave = new wumpusWorldCell* [m];
for(int i = 0; i < m; i++)
   cave[i] = new wumpusWorldCell [n];}


My member function that attempts to access the struct:

void wumpusWorld::initializeWumpusWorld(int m, int n){
for(int i = 0; i < m; i++)
   for(int j = 0; j < n; j++)
       cave[m][n].state = '?';
return;}

All code compiles and links just fine.  The program crashes on the line "cave[m][n].state = '?';".  What am I missing or not understanding?
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CABrown
CABrown

ASKER

Thanks for pointing out what should have been obvious to me!  I should have just gotten some rest first!  Thanks!