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(i nt 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::initializeWum pusWorld(i nt 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?
My struct:
struct wumpusWorldCell{
char state;
bool wumpus;
bool gold;
};
My class:
class wumpusWorld{
wumpusWorldCell **cave;
...
};
My constructor that creates the array:
wumpusWorld::wumpusWorld(i
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::initializeWum
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER