Solved

Dynamically allocated array of structs

Posted on 2003-11-01
2
339 Views
Last Modified: 2010-04-01
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?
0
Comment
Question by:CABrown
2 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 300 total points
ID: 9665205
   for(i = 0; i < m; i++)
        for(j = 0; j < n; j++)
            cave[i][j].state = '?';


cave[i][j] and not cave[m][n]
0
 

Author Comment

by:CABrown
ID: 9665236
Thanks for pointing out what should have been obvious to me!  I should have just gotten some rest first!  Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now