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
Solved

Dynamically allocated array of structs

Posted on 2003-11-01
2
341 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MSVCR80.dll crash 2 141
Which IDE to use to begin C++ training? 5 65
PDF library for Delphi 2 128
Header of docx file 17 114
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…
  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 video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

808 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