I have this 2d dynamic array and I want to pass it to a function, How would I go about doing that
int ** board;
board = new int*[boardsize];
//creates a multi dimensional dynamic array
for(int i = 0; i < boardsize; i++)
{
board[i] = new int[boardsize];
}
int **board;
board = new int*[boardsize];
for (int i = 0; i < boardsize; i++)
board[i] = new int[size];
You need to allocate the second depth of array.
To pass this 2D array to a function implement it like:
fun1(int **);
Check the 2D array implementation on below link:
http://www.codeproject.com/Articles/21909/Introduction-to-dynamic-two-dimensional-arrays-in
You should define a function to take this kind of argument
void func(int **board) {
for (int i=0; i<boardsize; ++i) {
board[i] = new int [size];
}
}
func(board);
If boardsize
or size
are not globlal, you can pass it through parameters.
void func(int **board, int boardsize, int size) {
for (int i=0; i<boardsize; ++i) {
board[i] = new int [size];
}
}
func(board, boardsize, size);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With