Solved

Dynamically allocated array of structs

Posted on 2003-11-01
2
338 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
Comment Utility
   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
Comment Utility
Thanks for pointing out what should have been obvious to me!  I should have just gotten some rest first!  Thanks!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

743 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

18 Experts available now in Live!

Get 1:1 Help Now