My friend and I are working on a battleship program. We want to use a player struct and two variables (player one, player two) so that we can use the same functions and just pass one or two in depending on our needs.
We are getting a segmentation fault (core dump) at line 55 where it uses **which.board instead of **board. What's going wrong?
Its because you never actually modify 'which's board variable. Try changing your initialize_board1 function to this:
1 2 3 4
which.board = newchar* [11];
for (int i = 0; i < 11; ++i)
which.board[i] = newchar[11];
...
and your player class to this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class player {
int row;
int col;
char** board; // we don't want a two dimensional array of pointer pointers
// initialize board to not point to anything
player() : row(0), col(0), board(nullptr) {}
// add a destructor to clean up when your program is done.
~player() {
if (!board) return; // board was never allocated
for (int i = 0; i < 11; ++i)
delete[] board[i];
delete[] board;
}
};
EDIT:
@Cody0023: Pointer to Pointers = Pointer to arrays = arrays of arrays
Basically, @altenhos is using 2 Dimensional arrays.