I want to create a 2D pointer boolean array, but as soon as I declare it and try to initialize it I get a segmentation fault. I tried only declaring the array and not initializing it, and I've tried initializing the array as well.
I have some functions and global variables in my program, and have tried making everything except my main function, #include , and using namespace std; into a comment but I'm still getting the error: only declaring:
int main(){
// Variable declarations. You can add more if necessary
bool **world;
int nrows, ncols;
char next;
cout << "Enter world dimensions (rows and columns): ";
cin >> nrows >> ncols;
**world = new bool*[ncols];
for (int i = 0; i < ncols; i++) {
world[i] = new bool[nrows];
}
declaring and initializing:
int main(){
// Variable declarations. You can add more if necessary
bool **world;
int nrows, ncols;
char next;
cout << "Enter world dimensions (rows and columns): ";
cin >> nrows >> ncols;
**world = new bool*[ncols];
for (int i = 0; i < ncols; i++) {
world[i] = new bool[nrows];
}
for (int i = 0; i < ncols; ++i) {
for (int j = 0; j < nrows; ++j) {
world[i][j] = false;
}
}
The error is: Segmentation fault (core dumped)
.