C++ Pointers To Arrays In Functions
First of all sorry if this is a question that could be googled but I've read multiple articles and some did cover some parts of what I inteded to do but none covered all of them together :S
I would like to pass a pointer to a 3-dimensional booleaan array into a function and then be able to acces the array through that pointer.
The array declaration is like this; bool Array[9][9][9]
Somehow Visual c++ 2003 keeps giving me errors that the variable types arent correct.
I would appreciate all help,
Thanks in advance...
Quinnie
void foo(bool*** bar);or void foo(bool bar[][][]);
to use
int main(....{ bool arr[9][9][9];foo(arr);....
Thanks, I'll try that right away...
Edit: It doesnt seem to work, I'll post my script and erros...
Sudoku Solver.cpp(16) : error C2664: 'LoadSudoku' : cannot convert parameter 1 from 'bool [9][9][9]' to 'bool *** '
So something seems to be wrong... (I'm rewriting my sudoku solver from qbasic into c++ for the people wondering about all the Sudoku stuff :P)
Edit: It doesnt seem to work, I'll post my script and erros...
Sudoku Solver.cpp(16) : error C2664: 'LoadSudoku' : cannot convert parameter 1 from 'bool [9][9][9]' to 'bool *** '
int _tmain(){bool SUD[9][9][9]; LoadSudoku( SUD );}int LoadSudoku( bool*** Sudoku ){}
So something seems to be wrong... (I'm rewriting my sudoku solver from qbasic into c++ for the people wondering about all the Sudoku stuff :P)
class SudokuBoard { bool board[9][9][9];public: friend istream& operator>>(istream& in, SudokuBoard & loaded) { // load the board } friend ostream& operator<<(ostream& out, SudokuBoard & displayed const) { // display the solved board } bool Solve() { // solve the board }}
First of all Thank You for the alternative solution... though I'm not a big fan of using classes and stuff... Though if there is no other solution to this problem I'll guess I should get used to it :P
But I would prefer a solution that would pass the pointer using a function above using classes to do it...
Thanks for all of the help...
But I would prefer a solution that would pass the pointer using a function above using classes to do it...
Thanks for all of the help...
Quote:Original post by Quinnie
Thanks, I'll try that right away...
Edit: It doesnt seem to work, I'll post my script and erros...
Sudoku Solver.cpp(16) : error C2664: 'LoadSudoku' : cannot convert parameter 1 from 'bool [9][9][9]' to 'bool *** '
*** Source Snippet Removed ***
So something seems to be wrong... (I'm rewriting my sudoku solver from qbasic into c++ for the people wondering about all the Sudoku stuff :P)
Sorry about.
hmmm the following will work for your sitation
Quote:
void foo(bool bar[9][9][9]);
If nobody posts the answer I will repost here.
If you don't want to use stl::vector, then there are two (main) ways accept multi-dimensional arrays as function arguments, and the method to choose depends on how the array was allocated. Option 2 may seem much more complicated, but it has the advantage of the rows/cols/depth being dynamic.
// Option 1:void func1(bool array[][9][9]){ // array properties: // num rows: can be any length // num cols: must be 9 elements // num deep: must be 9 elements}void usage(){ bool array[9][9][9]; func1(array);}
// Option 2:void func1(bool ***array){ // array properties: // num rows: can be any length // num cols: can be any length // num deep: can be any length}void usage(){ const int NUM_ROWS = 9; const int NUM_COLS = 9; const int NUM_DEEP = 9; // allocate dynamic 3D array bool ***array = new bool**[NUM_ROWS]; for (int i = 0; i < NUM_ROWS; i++) { array = new bool*[NUM_COLS]; for (int j = 0; j < NUM_COLS; j++) { array[j] = new bool[NUM_DEEP]; } } func1(array); // free 3D array for (int i = 0; i < NUM_ROWS; i++) { for (int j = 0; j < NUM_COLS; j++) { delete [] array[j]; } delete [] array; } delete [] array;}
Thanks a lot for all your guys help...
I think I'll stick to the first method also mentioned by the AP since I wont be needing dynamic size of the array.
Again thanks to you all...
I think I'll stick to the first method also mentioned by the AP since I wont be needing dynamic size of the array.
Again thanks to you all...
void function(bool (&array)[9][9][9]){} (pass by reference to bool[9][9][9]) if you want to enforce all three array dimensions.
Σnigma
Σnigma
Quote:Original post by Enigma
void function(bool (&array)[9][9][9]){} (pass by reference to bool[9][9][9]) if you want to enforce all three array dimensions.
Σnigma
Arrays are passed by reference by default. For example, 'array' in the following code is passed by reference.
void func(bool array[9][9][9]){ }
EDIT: Enigma, I think I misunderstood your quote. I believe you were emphasizing the enforcement of dimensions instead of passing by reference.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement