Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dynamically allocated array of structs

Posted on 2003-11-01
2
Medium Priority
?
346 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 1200 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

715 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