Jump to content
  • Advertisement
Sign in to follow this  
Quinnie

C++ Pointers To Arrays In Functions

This topic is 4230 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster

void foo(bool*** bar);
or
void foo(bool bar[][][]);



to use

int main(....
{
bool arr[9][9][9];
foo(arr);
....


Share this post


Link to post
Share on other sites
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 *** '


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)

Share this post


Link to post
Share on other sites

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
}
}

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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;

}

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!